cbo*_*opp 30 mongodb mongodb-query aggregation-framework
我有一个集合"帐户",其中包含类似于此结构的文档:
{
"email" : "john.doe@acme.com",
"groups" : [
{
"name" : "group1",
"contacts" : [
{ "localId" : "c1", "address" : "some address 1" },
{ "localId" : "c2", "address" : "some address 2" },
{ "localId" : "c3", "address" : "some address 3" }
]
},
{
"name" : "group2",
"contacts" : [
{ "localId" : "c1", "address" : "some address 1" },
{ "localId" : "c3", "address" : "some address 3" }
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
通过
q = { "email" : "john.doe@acme.com", "groups" : { $elemMatch: { "name" : "group1" } } }
p = { "groups.name" : 0, "groups" : { $elemMatch: { "name" : "group1" } } }
db.accounts.find( q, p ).pretty()
Run Code Online (Sandbox Code Playgroud)
我将成功获得我感兴趣的指定帐户的组.
问题:如何在指定"帐户"的某个"组"中获得有限的"联系人"列表?我们假设我有以下参数:
鉴于这些论点,我希望得到以下结果:
{
"groups" : [
{
"name" : "group1", (might be omitted)
"contacts" : [
{ "localId" : "c1", "address" : "some address 1" },
{ "localId" : "c3", "address" : "some address 3" }
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
除了最终的联系人之外,我不需要任何其他内容.
途径
为简单起见,所有查询都尝试仅提取一个匹配的联系人而不是匹配的联系人列表.我尝试了以下查询但没有成功:
p = { "groups.name" : 0, "groups" : { $elemMatch: { "name" : "group1", "contacts" : { $elemMatch: { "localId" : "c1" } } } } }
p = { "groups.name" : 0, "groups" : { $elemMatch: { "name" : "group1", "contacts.localId" : "c1" } } }
not working: returns whole array or nothing depending on localId
p = { "groups.$" : { $elemMatch: { "localId" : "c1" } } }
error: {
"$err" : "Can't canonicalize query: BadValue Cannot use $elemMatch projection on a nested field.",
"code" : 17287
}
p = { "groups.contacts" : { $elemMatch: { "localId" : "c1" } } }
error: {
"$err" : "Can't canonicalize query: BadValue Cannot use $elemMatch projection on a nested field.",
"code" : 17287
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!
Nei*_*unn 37
如此好的问题值得现代回应.所请求的数组过滤实际上可以通过简单$match和$project流水线阶段在3.2版的现代MongoDB版本中完成,就像原始的普通查询操作所要求的那样.
db.accounts.aggregate([
{ "$match": {
"email" : "john.doe@acme.com",
"groups": {
"$elemMatch": {
"name": "group1",
"contacts.localId": { "$in": [ "c1","c3", null ] }
}
}
}},
{ "$addFields": {
"groups": {
"$filter": {
"input": {
"$map": {
"input": "$groups",
"as": "g",
"in": {
"name": "$$g.name",
"contacts": {
"$filter": {
"input": "$$g.contacts",
"as": "c",
"cond": {
"$or": [
{ "$eq": [ "$$c.localId", "c1" ] },
{ "$eq": [ "$$c.localId", "c3" ] }
]
}
}
}
}
}
},
"as": "g",
"cond": {
"$and": [
{ "$eq": [ "$$g.name", "group1" ] },
{ "$gt": [ { "$size": "$$g.contacts" }, 0 ] }
]
}
}
}
}}
])
Run Code Online (Sandbox Code Playgroud)
这使得$filter和$map运算符只能在满足条件的情况下从数组中返回元素,并且性能远远优于使用$unwind.由于管道阶段有效地反映了操作中"查询"和"项目"的结构.find(),因此这里的性能与此类操作基本相同.
请注意,如果目的是实际"跨文档"工作以将详细信息从"多个"文档而不是"一个"中提取出来,那么这通常需要某种类型的$unwind操作才能这样做,因此启用数组项可以进行"分组".
这基本上是方法:
db.accounts.aggregate([
// Match the documents by query
{ "$match": {
"email" : "john.doe@acme.com",
"groups.name": "group1",
"groups.contacts.localId": { "$in": [ "c1","c3", null ] },
}},
// De-normalize nested array
{ "$unwind": "$groups" },
{ "$unwind": "$groups.contacts" },
// Filter the actual array elements as desired
{ "$match": {
"groups.name": "group1",
"groups.contacts.localId": { "$in": [ "c1","c3", null ] },
}},
// Group the intermediate result.
{ "$group": {
"_id": { "email": "$email", "name": "$groups.name" },
"contacts": { "$push": "$groups.contacts" }
}},
// Group the final result
{ "$group": {
"_id": "$_id.email",
"groups": { "$push": {
"name": "$_id.name",
"contacts": "$contacts"
}}
}}
])
Run Code Online (Sandbox Code Playgroud)
这是对多个匹配的"数组过滤",其基本投影功能.find()无法做到.
您有"嵌套"数组,因此需要处理$unwind两次.与其他操作一起.
您可以使用聚合框架的$unwind运算符。例如:
db.contact.aggregate({$unwind:'$groups'}, {$unwind:'$groups.contacts'}, {$match:{email:'john.doe@acme.com', 'groups.name':'group1', 'groups.contacts.localId':{$in:['c1', 'c3', 'whatever']}}});
Run Code Online (Sandbox Code Playgroud)
应该给出以下结果:
{ "_id" : ObjectId("5500103e706342bc096e2e14"), "email" : "john.doe@acme.com", "groups" : { "name" : "group1", "contacts" : { "localId" : "c1", "address" : "some address 1" } } }
{ "_id" : ObjectId("5500103e706342bc096e2e14"), "email" : "john.doe@acme.com", "groups" : { "name" : "group1", "contacts" : { "localId" : "c3", "address" : "some address 3" } } }
Run Code Online (Sandbox Code Playgroud)
如果您只需要一个对象,则可以使用$group运算符。
| 归档时间: |
|
| 查看次数: |
15351 次 |
| 最近记录: |