猫鼬获取本地化多语言

Mongoose fetch localization multilingual

我将 nodejs 与 mongoose (mongodb) 一起使用,我想在子文档数组中过滤语言 selected.

用户架构:

var localUserSchema = new mongoose.Schema({
firstName: {
    type: String
},
moreInformation: {
    experience: {
        type: Number
    },
    specializations: [{
        ...
        sports:[{
        type: Schema.Types.ObjectId, 
        ref: 'sport'
        }]
    }]
});

用户数据:

    [{
    "_id": {
        "$oid": "5fc6a379b1d5ff2c42a9a536"
    },
    "moreInformation": {
        "experience" : 2,
        "specializations": [{
            "sports": [{
                "$oid": "5fc6aa91b1db6cd15702241c"
            }, {
                "$oid": "5fcb741e786f0703646befe2"
            }]
        }]
    }
    }]

运动模式:

var sportSchema = new Schema({ 
    name: {
       en: {
           type: String
       },
       it: {
           type: String
       }
   },  
   icon: {
       type: Schema.Types.ObjectId, ref: 'file'
   }
}); 

运动数据:

[{
      "_id": {
        "$oid": "5fc6aa91b1db6cd15702241c"
      },
      "name": {
        "en": "Football",
        "it": "Calcio"
      },
      "icon": {
        "$oid": "5fc9598a0955177dee8a3bc4"
      }
    },{
      "_id": {
        "$oid": "5fcb741e786f0703646befe2"
      },
      "name": {
        "en": "Swimming",
        "it": "Nuoto"
      },
      "icon": {
        "$oid": "5fc9598a0955177dee8a3bc5"
      }
    }

我希望所有用户都通过运动 ID 加入运动,但按语言键过滤,如下所示,并将运动名称语言替换为不带语言键的名称 selected。

所以,如果我想 select 并过滤英语 'en',我想得到这个结果:

[
{
    "_id": "5fc6a379b1d5ff2c42a9a536",
    "moreInformation": {
        "specializations": [{
            ...
            "sports": [
                {
                    "_id": "5fc6aa91b1db6cd15702241c",
                    "name": "Football",
                    "icon": "5fc9598a0955177dee8a3bc4"
                },
                {
                    "_id": "5fcb741e786f0703646befe2",
                    "name": "Swimming",
                    "icon": "5fc9598a0955177dee8a3bc5"
              }]
        }]
        }
    }
}

我该怎么做?

您将需要使用 aggregation methods like $unwind, $lookup, $group last but not least $project

他们定义了一系列步骤来帮助您按预期检索数据

获取您想要的响应查看以下代码和 运行 示例 in here

Ps:我建议您查看 mongo 游乐场(上面的 link)中的示例并分离聚合管道以更好地理解过程

db.users.aggregate([
  {
    // move specializations array to separated objects
    "$unwind": "$moreInformation.specializations" 
  },
  {
    // move specialization.sports to separated objects
    "$unwind": "$moreInformation.specializations.sports"
  },
  {
    // search into sports collection based on the temporary "sports" attribute
    $lookup: {
      from: "sports",
      localField: "moreInformation.specializations.sports",
      foreignField: "_id",
      as: "fullSport"
    }
  },
  {
    // as the lookup resolves an array of a single result we move it to be an object
    "$unwind": "$fullSport"
  },
  {
    // here we select only the attributes that we need and the selected language
    "$project": {
      "moreInformation.experience": 1,
      "moreInformation.specializations.sports._id": "$fullSport._id",
      "moreInformation.specializations.sports.icon": "$fullSport.icon",
      "moreInformation.specializations.sports.name": "$fullSport.name.en"
    }
  },
  {
    // then we group it to make the separated objects an array again
    "$group": {
      "_id": "$_id",
      // we group by the $_id and move it to temporary "sports" attribute
      "sports": {
        $push: "$moreInformation.specializations.sports"
      },
      "moreInformation": {
        $first: "$moreInformation"
      }
    }
  },
  {
    $project: {
      "moreInformation.experience": 1,
      // move back the temporary "sports" attribute to its previous path
      "moreInformation.specializations.sports": "$sports"
    }
  }
])