英文:
Filtering by non-null values on a nested array does not return results
问题
我有一个包含类似以下文档的集合:
{
country: "NL",
product: "1583392",
prices: [{
price: 220.00,
rrp: 250.00,
version: 10
}, {
price: null,
rrp: 250.00,
version: 9
}]
}
我试图使用country
和product
查找这个文档,但仅当prices
中第一个条目的price
不为null时才查找。所以我尝试使用以下查询,但它没有返回结果:
{ country: "NL", product: "1583392", "prices.0.price": { $ne: null } }
=> 未找到文档 (?)
然后我测试了一些其他查询,其中一些导致意外的结果:
{ country: "NL", product: "1583392", "prices.0.price": { $eq: null }}
=> 找到文档 (?)
{ country: "NL", product: "1583392", "prices.0.price": null }
=> 找到文档 (?)
{ country: "NL", product: "1583392", "prices.0.price": 220.00 }
=> 找到文档
{ country: "NL", product: "1583392", "prices.0.rrp": null }
=> 找到文档 (?)
{ country: "NL", product: "1583392", "prices.0.rrp": 250.00 }
=> 找到文档
有没有办法匹配嵌套数组中特定项目的字段为null?
(使用$elemMatch
似乎可以正确工作,但我需要匹配只有第一个嵌套项。)
英文:
I have a collection with documents like this:
{
country: "NL",
product: "1583392",
prices: [{
price: 220.00,
rrp: 250.00,
version: 10
}, {
price: null,
rrp: 250.00,
version: 9
}]
}
What I'm attempting to do is find this document using the country
and product
, but only if the price
of the first entry in prices
is not null. So what I'm trying to use is this query, but it returns no result:
{ country: "NL", product: "1583392", "prices.0.price": { $ne: null } }
=> document not found (?)
I then tested some other queries, where some lead to unexpected results:
{ country: "NL", product: "1583392", "prices.0.price": { $eq: null }}
=> document found (?)
{ country: "NL", product: "1583392", "prices.0.price": null }
=> document found (?)
{ country: "NL", product: "1583392", "prices.0.price": 220.00 }
=> document found
{ country: "NL", product: "1583392", "prices.0.rrp": null }
=> document found (?)
{ country: "NL", product: "1583392", "prices.0.rrp": 250.00 }
=> document found
Is there any way to match a field of a specific item in a nested array by null?
(Using $elemMatch
seems to work correctly, but I need to match only the first nested item.)
答案1
得分: 1
通过$type查询有效。
{ country: "NL", product: "1583392", "prices.0.price": { $not: { $type: 10 } } }
类型10
是null
的BSON类型。
英文:
Querying by $type works.
{ country: "NL", product: "1583392", "prices.0.price": { $not: { $type: 10 } } }
Type 10
is the BSON type for null
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论