如何通过查询子文档将数组用作过滤器

问题描述 投票:2回答:1

我的mongodb集合上有以下文档结构:

{
"_id" : "5Ci9sLeBu2iPbWtR5",
"productId" : "010101111",
"description" : "PRODUCT EXAMPLE REF 1001",
"prices" : [ 
    {
        "priceId" : 10,
        "description" : "Promotions",
        "price" : 97.99
    },
    {
        "priceId" : 15,
        "description" : "Retail list",
        "price" : 105.65
    },
    {
        "priceId" : 20,
        "description" : "Standard list",
        "price" : 109.10
    }
]}

我想要的只是查询priceIds的特定数组,例如:[10,20],结果:

{
"_id" : "5Ci9sLeBu2iPbWtR5",
"productId" : "010101111",
"description" : "PRODUCT EXAMPLE REF 1001",
"prices" : [ 
    {
        "priceId" : 10,
        "description" : "Promotions",
        "price" : 97.99
    },
    {
        "priceId" : 20,
        "description" : "Standard list",
        "price" : 109.10
    }
]}

使用带有$ filter的$ in运算符(完美的虚构场景):

db.getCollection('products').aggregate([
{$match: { "productId":"010101111" }},
{$project: { 
    "prices": {
        $filter: {
            input: "$prices",
            as: "price",
            cond: { $in: ["$$price.priceId",[10, 20]] }
        }
    }
}}])

它不起作用,因为mongodb抱怨$ in运算符(“无效运算符'$ in'”)。

当然,我可以通过$ unwind来做到这一点,但我会遇到性能问题,因为毕竟我需要再次组合。

我在问题中找到的最接近的答案是这两个:

  1. Retrieve only the queried element in an object array in MongoDB collection
  2. Filter array using the $in operator in the $project stage

但它们都没有关于在子文档中使用数组过滤器进行搜索。

mongodb mongodb-query aggregation-framework
1个回答
1
投票

$in运算符仅在MongoDB 3.4+的聚合管道中有效。但是,解决方法涉及使用集合运算符。使用$setIsSubset作为替换,如果第一组的所有元素都出现在第二组中,则返回true,包括第一组等于第二组时:

cond: { $setIsSubset: [["$$price.priceId"],[10, 20]] }
© www.soinside.com 2019 - 2024. All rights reserved.