【问题标题】:How to return Objects with empty sub Array document if sub Array documents don't satisfy the match condition?如果子数组文档不满足匹配条件,如何返回具有空子数组文档的对象?
【发布时间】:2019-10-14 06:34:07
【问题描述】:

如果子数组文档不满足 mongoose 中的匹配条件,Mongoose 不会返回对象。

MongoDB 查询:

db.fruits.aggregate(        
    { $match: { "isActive": true, fruitId: "kipld" } },
    { $unwind: '$types' },
    { $match: { "types.isActive": true } },
    { $group: {
            _id: '$_id', 
            name: {$first: '$name'}, 
            price: { $first: '$price' },
            types : { $push : '$types' }
        }
    },
    { $project: {
            '_id': 1, 
            'name':1, 
            'price': 1, 
            'types': 1,
        }
    }
)

MongoDB 集合:

{
    _id: "abcdefg",
    fruitId: "kipld",
    isActive: true,
    types :[
        {
            name: "Kashmir Apple",
            price: 90,
            isActive: false
        },
        {
            name: "American Apple",
            price: 120,
            isActive: false
        }
    ]
}

预期结果如下:

{
    _id: "abcdefg",
    fruitId: "kipld",
    isActive: true,
    types :[]
}

但是当我运行上述查询时,我什么也没得到。

【问题讨论】:

  • 您的查询显示{ $match: { "types.isActive": true } },但输入文档的"tyoes.isActive" 值为false。因此,没有选择任何文档。
  • 这是预期的行为。
  • 如果不存在true 记录,我可以这样做以返回EMPTY 数组吗?
  • 您可以在这里查看:stackoverflow.com/questions/3985214/…
  • @Rajath 你可以。但是,与您正在使用的查询管道无关。

标签: mongodb mongoose


【解决方案1】:

您可以使用$filter 聚合运算符获得所需的结果。这只是过滤所有数组元素并返回具有匹配过滤条件的数组。请注意,过滤器应用于每个文档的数组。因此,在您的情况下,$unwind$group 阶段不需要来获得结果。

样本fruits 收藏:

{
        "_id" : "abcdefg",
        "fruitId" : "kipld",
        "isActive" : true,
        "types" : [
                {
                        "name" : "Kashmir Apple",
                        "price" : 90,
                        "isActive" : false
                },
                {
                        "name" : "American Apple",
                        "price" : 120,
                        "isActive" : false
                }
        ]
}
{
        "_id" : "uvwxyz",
        "fruitId" : "kipld",
        "isActive" : true,
        "types" : [
                {
                        "name" : "Kinnaur Apple",
                        "price" : 80,
                        "isActive" : false
                },
                {
                        "name" : "Shimla Apple",
                        "price" : 70,
                        "isActive" : true
                }
        ]
}

聚合查询:

db.fruits.aggregate([
  { $match: { "isActive": true, fruitId: "kipld" } },
  { $project: { _id: 1, fruitId: 1, isActive: 1, 
                result_types: { $filter: {
                                   input: "$types",
                                      as: "type",
                                    cond: { $eq: [ "$$type.isActive", true ] }
                                 } 
                             } } 
   }
])

输出:

注意“result_types”空数组,其中“types”数组的所有元素的types.isActive 值为false。结果输出中只有值为true 的元素。

{
        "_id" : "abcdefg",
        "fruitId" : "kipld",
        "isActive" : true,
        "result_types" : [ ]
}
{
        "_id" : "uvwxyz",
        "fruitId" : "kipld",
        "isActive" : true,
        "result_types" : [
                {
                        "name" : "Shimla Apple",
                        "price" : 70,
                        "isActive" : true
                }
        ]
}

【讨论】:

  • 谢谢,prasad 我用同样的方式实现了它
猜你喜欢
  • 2019-01-16
  • 2019-07-09
  • 2021-01-09
  • 1970-01-01
  • 2019-01-12
  • 2020-12-11
  • 1970-01-01
  • 2022-01-09
  • 2017-04-23
相关资源
最近更新 更多