首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >猫鼬获取本地化多语种

猫鼬获取本地化多语种
EN

Stack Overflow用户
提问于 2020-12-20 19:38:52
回答 1查看 316关注 0票数 0

我使用nodejs和mongoose (mongodb),并希望在子文档数组中筛选所选语言。

用户模式:

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

用户数据:

代码语言:javascript
复制
    [{
    "_id": {
        "$oid": "5fc6a379b1d5ff2c42a9a536"
    },
    "moreInformation": {
        "experience" : 2,
        "specializations": [{
            "sports": [{
                "$oid": "5fc6aa91b1db6cd15702241c"
            }, {
                "$oid": "5fcb741e786f0703646befe2"
            }]
        }]
    }
    }]

体育模式:

代码语言:javascript
复制
var sportSchema = new Schema({ 
    name: {
       en: {
           type: String
       },
       it: {
           type: String
       }
   },  
   icon: {
       type: Schema.Types.ObjectId, ref: 'file'
   }
}); 

体育数据:

代码语言:javascript
复制
[{
      "_id": {
        "$oid": "5fc6aa91b1db6cd15702241c"
      },
      "name": {
        "en": "Football",
        "it": "Calcio"
      },
      "icon": {
        "$oid": "5fc9598a0955177dee8a3bc4"
      }
    },{
      "_id": {
        "$oid": "5fcb741e786f0703646befe2"
      },
      "name": {
        "en": "Swimming",
        "it": "Nuoto"
      },
      "icon": {
        "$oid": "5fc9598a0955177dee8a3bc5"
      }
    }

我希望所有的用户加入体育通过体育id,但过滤的语言键如下,并取代体育名称语言的名称选择没有语言键。

所以,如果我想选择和过滤英语的'en',我想得到这样的结果:

代码语言:javascript
复制
[
{
    "_id": "5fc6a379b1d5ff2c42a9a536",
    "moreInformation": {
        "specializations": [{
            ...
            "sports": [
                {
                    "_id": "5fc6aa91b1db6cd15702241c",
                    "name": "Football",
                    "icon": "5fc9598a0955177dee8a3bc4"
                },
                {
                    "_id": "5fcb741e786f0703646befe2",
                    "name": "Swimming",
                    "icon": "5fc9598a0955177dee8a3bc5"
              }]
        }]
        }
    }
}

我怎么能做到呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-21 23:42:23

您将需要使用聚合方法,如$unwind$lookup$group最后但并非最不重要的$project

它们定义了一系列步骤,以帮助您按预期检索数据。

获取您想要的响应,查看下面的代码和正在运行的示例在这里

Ps:我建议您查看mongo操场中的示例(上面的链接),并将聚合管道分开,以更好地理解过程。

代码语言:javascript
复制
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"
    }
  }
])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65384043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档