【发布时间】:2015-11-18 06:34:24
【问题描述】:
对于下面的文档,我需要输出
1. image arrays which are hsl:1
2. dim.hsl embedded document
主文档
{
"_id" : ObjectId("564b17873b91989fcb4e9707"),
"type" : "article",
"image" : [
{
"name" : "i3",
"hsl" : 1
},
{
"name" : "i1",
"hsl" : 1
},
{
"name" : "i2",
"hsl" : 0,
"ai" : 1
}
],
"dim" : {
"hsl" : {
"path" : "blah"
},
"ai" : {
"path" : "blah"
}
}
我的预期输出如下,它从图像数组和 dim.hsl 嵌入文档中返回 2 个文档
{
"result" : [
{
"_id" : ObjectId("564b17873b91989fcb4e9707"),
"type" : "article",
"image" : [
{
"name" : "i3",
"hsl" : 1
},
{
"name" : "i1",
"hsl" : 1
}
],
"dim" : {
"hsl" : {
"path" : "blah"
}
}
}
],
"ok" : 1.0000000000000000
}
下面尝试的代码是不返回 dim.hsl 嵌入文档。 该逻辑作为内联包含在代码中。 我不确定 检查节点存在 的 mongodb 表达式。所以我使用了 {$not : {$not : '$hsl'}}。 (希望有更好的方法) 请提出建议。
db.survey.aggregate([
{ $match: {
image: { $elemMatch: {hsl: 1}}, // docs which have image.hsl
'dim.hsl': { $exists: true} // docs which have dim.hsl
}},
{ $redact : {
$cond: {
if: { $or : [
{$eq: ['$hsl',1]}, // for image array document with hsl:1
{$eq : ['$type' ,'article']},// for main document node
{$not : {$not : '$hsl'}} // for dim -- NOT WORKING
]},
then: "$$DESCEND",
else: "$$PRUNE"
}
}}]);
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework