如何在聚合中包含集合数据

时间:2015-10-26 20:30:26

标签: mongodb aggregation-framework

我有以下MongoDB文档结构:

user: {
  name: 'user name',
  email: 'email@gmail.com',
  isBlocked: false,
  cvs: [{
    title: "role title",
    technologies: [
      {name: 'JavaScript'}
      {name: 'NodeJs'}
   ],
    agents: [
     {
      _id:'some id1',
      contactRequest: true
     },
     {
      _id:'some id2',
      contactRequest: false
     }
    ],
  }]
}

如果我通过对嵌入文档的属性进行分组来执行聚合,如何将user.name添加到输出中,例如

db.users.aggregate([
    {
        "$match": {
            "cvs.isBlocked": false,
            "cvs.moderated": true,
            "cvs.isVisible": true,
            "cvs.technologies.main": true 
        }
    },
    {"$unwind": "$cvs"},
    {"$unwind": "$cvs.technologies"},
    {
        "$match": {
            "cvs.isBlocked": false,
            "cvs.moderated": true,
            "cvs.isVisible": true,
            "cvs.technologies.main": true 
        }
    },
    {
        "$group": {
            "_id": {
                "type": "$cvs.occupationType",
                "proficiency": "$cvs.proficiencyLevel",
                "_id": "$cvs._id",
                "title": "$cvs.title"               
            },
            "technologies": {
                "$push": "$cvs.technologies.text"
            }
        }
    },
    {
        "$project": {
            "type": "$_id.type",
            "proficiency": "$_id.proficiency",
            "_id": "$_id._id",
            "title": "$_id.title",
            "technologies": 1,
        }
    }
])

在这种情况下,$$ ROOT指的是$ cvs。

此外,我希望仅当user.nameemailuser.isBlockedfalse时才会加入agent.contactRequesttrue

0 个答案:

没有答案