【发布时间】:2015-10-25 21:10:55
【问题描述】:
我的文档结构如下:
{
name: "some user name",
cvs: [{
title: 'Cv title'
technologies: [
{
text: 'JavaScript',
main: true
},
{
text: "AngularJs",
main: true
}
]
}]
}
当我进行以下聚合时(db 中只有一个文档):
db.users.aggregate([
{"$unwind": "$cvs"},
{"$unwind": "$cvs.technologies"},
{"$match": {
"cvs.isBlocked": false,
"cvs.moderated": true,
"cvs.isVisible": true,
"cvs.technologies.main": true
}
},
{"$project": {
"type": "$cvs.occupationType",
"proficiency": "$cvs.proficiencyLevel",
"_id": "$cvs._id",
"title": "$cvs.title",
"technologies": "$cvs.technologies.text",
}}
])
我得到一个包含两个元素的数组(但这是同一个文档),只是因为有两种技术匹配
"cvs.technologies.main": true
[
{
"_id" : ObjectId("5629e813279b62fe075fbd4c"),
"type" : "Frontend",
"proficiency" : "Middle",
"title" : "Frontend developer",
"technologies" : "JavaScript",
},
{
"_id" : ObjectId("5629e813279b62fe075fbd4c"),
"type" : "Frontend",
"proficiency" : "Middle",
"title" : "Frontend developer",
"technologies" : "Ruby",
}
]
我怎样才能得到这个结果?:
[
{
"_id" : ObjectId("5629e813279b62fe075fbd4c"),
"type" : "Frontend",
"proficiency" : "Middle",
"title" : "Frontend developer",
"technologies" : ["JavaScript", "Ruby"],
}
]
【问题讨论】:
-
请显示与您收藏的文档结构相同的示例文档。我认为有更好的方法来做到这一点。
标签: mongodb mongodb-query database