【发布时间】:2020-12-02 16:13:44
【问题描述】:
这是我在 Mongo DB Compass 中的 Json。我只是从每个集合中查询大于评级的产品。
注意:如果我使用 pageCount,它可以正常工作,因为它不在集合中。
{PageCount:{gte:2}} -- works.
如果有人匹配集合的内部数组集合,则会显示全部。
当我们执行以下查询时,如果索引中的任何一个大于 99,它会显示所有值。
{"ProductField.ProductDetailFields.ProductDetailInfo.ProductScore.Rating": {$exists:true, $ne: null , $gte: 99}}
如何像 foreach 一样迭代并检查 MongoDB 查询中的条件
{
"_id":{
"$oid":"5fc73a7b3fb52d00166554b9"
},
"ProductField":{
"PageCount":2,
"ProductDetailFields":[
{
"PageNumber":1,
"ProductDetailInfo":[
{
"RowIndex":0,
"ProductScore":{
"Name":"Samsung",
"Rating":99
},
},
{
"RowIndex":1,
"ProductScore":{
"Name":"Nokia",
"Rating":96
},
},
{
"RowIndex":2,
"ProductScore":{
"Name":"Apple",
"Rating":80
},
}
]
}
]
}
},
{
"_id":{
"$oid":"5fc73a7b3fb52d0016655450"
},
"ProductField":{
"PageCount":2,
"ProductDetailFields":[
{
"PageNumber":1,
"ProductDetailInfo":[
{
"RowIndex":0,
"ProductScore":{
"Name":"Sony",
"Rating":93
}
},
{
"RowIndex":1,
"ProductScore":{
"Name":"OnePlus",
"Rating":93
}
},
{
"RowIndex":2,
"ProductScore":{
"Name":"BlackBerry",
"Rating":20
}
}
]
}
]
}
}
在 Mongo Shell 中运行此查询时 - 没有 sql 客户端抛出以下错误。我们正在使用 3.4.9 https://www.nosqlclient.com/demo/
【问题讨论】:
-
您希望从您的帖子中的该文档得到什么结果?顺便说一句
{"yourLongThing": {$gte: 99}}这已经足够了,因为任何大于 99 的数字都存在并且不为空 XD -
搜索将在多个文档上进行,因为在文档集合内考虑 2 个集合 - 每个文档 ProductDetailInfo 数组都有一个产品评级,我需要根据评级进行过滤。评级大于 99 的文档。如果您看到屏幕截图,我已经做了 gte:99,但它显示在值下方。如果它不在嵌套集合中,则 gte 作品
-
@Minsky 如果它按照你的方式工作,你可以把上面发布的 json 并尝试使用 gte 看看
标签: arrays mongodb mongodb-query aggregation-framework