我有以下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.name
为email
而user.isBlocked
为false
时才会加入agent.contactRequest
和true
。