仅检索我的状况在子数组中的所有元素
如何检索文件仅与子阵列中的所有元素相遇?
我的文档:
{
"_id" : ObjectId("5234cc89687ea597eabee675"),
"code" : "xyz",
"tags" : [
"school",
"book",
"bag",
"headphone",
"appliance"
],
"qty" : [
{
"size" : "S",
"num" : 10,
"color" : "blue"
},
{
"size" : "M",
"num" : 45,
"color" : "blue"
},
{
"size" : "M",
"num" : 60,
"color" : "blue"
},
{
"size" : "L",
"num" : 100,
"color" : "green"
}
]
}
{
"_id" : ObjectId("5234cc8a687ea597eabee676"),
"code" : "abc",
"tags" : [
"appliance",
"school",
"book"
],
"qty" : [
{
"size" : "6",
"num" : 100,
"color" : "green"
},
{
"size" : "6",
"num" : 50,
"color" : "blue"
},
{
"size" : "8",
"num" : 100,
"color" : "brown"
}
]
}
在此查询中,我重新编写了本文档,但我期望没有结果,因为在数组中,有一个具有60的元素... 阵列的所有元素必须符合条件,
db.getCollection('test').find({
qty: { $all: [
{ "$elemMatch" : { size: "M", num: { $lt: 50} } }
] }
} )
我的结果是:
{
"_id" : ObjectId("5234cc89687ea597eabee675"),
"code" : "xyz",
"tags" : [
"school",
"book",
"bag",
"headphone",
"appliance"
],
"qty" : [
{
"size" : "S",
"num" : 10,
"color" : "blue"
},
{
"size" : "M",
"num" : 45,
"color" : "blue"
},
{
"size" : "M",
"num" : 60,
"color" : "blue"
},
{
"size" : "L",
"num" : 100,
"color" : "green"
}
]
}
或在这种情况下仅导致符合条件的项目,但不是全部:
db.getCollection('test').aggregate([
{ $unwind: '$qty'},
{ $match: {'qty.size': {$eq: "M"}}},
{ $match: {'qty.num': {$lt: 50}}}
])
我的结果是:
{
"_id" : ObjectId("5234cc89687ea597eabee675"),
"code" : "xyz",
"tags" : [
"school",
"book",
"bag",
"headphone",
"appliance"
],
"qty" : {
"size" : "M",
"num" : 45,
"color" : "blue"
}
}
How to retrieve document only my condition meet all element in subarray?
my documents:
{
"_id" : ObjectId("5234cc89687ea597eabee675"),
"code" : "xyz",
"tags" : [
"school",
"book",
"bag",
"headphone",
"appliance"
],
"qty" : [
{
"size" : "S",
"num" : 10,
"color" : "blue"
},
{
"size" : "M",
"num" : 45,
"color" : "blue"
},
{
"size" : "M",
"num" : 60,
"color" : "blue"
},
{
"size" : "L",
"num" : 100,
"color" : "green"
}
]
}
{
"_id" : ObjectId("5234cc8a687ea597eabee676"),
"code" : "abc",
"tags" : [
"appliance",
"school",
"book"
],
"qty" : [
{
"size" : "6",
"num" : 100,
"color" : "green"
},
{
"size" : "6",
"num" : 50,
"color" : "blue"
},
{
"size" : "8",
"num" : 100,
"color" : "brown"
}
]
}
for this query I retreive this document but I expected no result, because in the array there is an element with the value 60...
all elements of the array must meet the condition
db.getCollection('test').find({
qty: { $all: [
{ "$elemMatch" : { size: "M", num: { $lt: 50} } }
] }
} )
my result is:
{
"_id" : ObjectId("5234cc89687ea597eabee675"),
"code" : "xyz",
"tags" : [
"school",
"book",
"bag",
"headphone",
"appliance"
],
"qty" : [
{
"size" : "S",
"num" : 10,
"color" : "blue"
},
{
"size" : "M",
"num" : 45,
"color" : "blue"
},
{
"size" : "M",
"num" : 60,
"color" : "blue"
},
{
"size" : "L",
"num" : 100,
"color" : "green"
}
]
}
Or in this situation only result a items that meet the condition, but not all :
db.getCollection('test').aggregate([
{ $unwind: '$qty'},
{ $match: {'qty.size': {$eq: "M"}}},
{ $match: {'qty.num': {$lt: 50}}}
])
my result is:
{
"_id" : ObjectId("5234cc89687ea597eabee675"),
"code" : "xyz",
"tags" : [
"school",
"book",
"bag",
"headphone",
"appliance"
],
"qty" : {
"size" : "M",
"num" : 45,
"color" : "blue"
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
$ MAP
创建一个布尔值辅助数组来对您的条件进行检查。之后,使用$ allelementStrue
执行过滤。这是 mongo playground 供您参考。
You can use
$map
to create an auxiliary array of booleans to perform the checking on your criteria. Afterwards, use$allElementsTrue
to perform the filtering.Here is the Mongo playground for your reference.