在嵌套数组上按非空值筛选不会返回结果。

huangapple go评论81阅读模式
英文:

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
    }]
}

我试图使用countryproduct查找这个文档,但仅当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 } } }

类型10nullBSON类型

英文:

Querying by $type works.

{ country: "NL", product: "1583392", "prices.0.price": { $not: { $type: 10 } } }

Type 10 is the BSON type for null.

huangapple
  • 本文由 发表于 2023年7月13日 17:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定