如何在Mongo的聚合管道的$ unwind阶段保留零长度值? [英] How can I retain zero-length values during the $unwind phase of the aggregate pipeline in Mongo?

查看:132
本文介绍了如何在Mongo的聚合管道的$ unwind阶段保留零长度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用聚合管道编写Mongo查询。在聚合期间,我需要 $ unwind 其中一个字段。但是,我不希望 $ unwind 排除具有该字段的零长度数组的条目,因为我仍然需要它们进一步沿着管道。

I'm writing a Mongo query using the aggregate pipeline. During the aggregation, I need to $unwind one of the fields. However, I don't want the $unwind to exclude entries that have zero-length arrays for that field, because I still need them further down the pipeline.

我的字段名为 items ,它是一个对象数组,每个对象包含两个值: quantity 价格。到目前为止,这是我的Mongo查询的片段:

My field is called items and it's an array of objects, which each contain two values: quantity and price. Here's a snippet of my Mongo query so far:

db.MyCollection.aggregate([
    {$match: ... },
    {$project: ... },
    // put another $project here to retain zero-length values? how to format it?
    {$project: {
            _id: "$$ROOT",
            items: {
                $ifNull: [
                    {
                        $literal: {
                            quantity: 0,
                            price: 0
                        }
                    }
                ]
            }
        }
    },
    {$unwind: "$items"},
    {$group: ... },
    {$project: ... },
    ...
]);

如您所见,这已经处理了文档没有<$ c的情况$ c> items 字段完全存在,在这种情况下,它会将其添加并为其提供一个空值来展开。

As you can see, this already handles the case where a document doesn't have the items field present at all, in which case it adds it in and gives it an empty value to unwind on.

什么是但是,处理的是已经存在 items 字段的文档,但它是空白的。我以为我可以使用 $ cond $ size 的某种组合来明确检查另一个的大小为零 $ project ,然后在这种情况下替换相同的文字,但 $ if 需要布尔值,所以我不是非常确定如何格式化。

What isn't handled, however, are documents that do already have that items field present, but it's blank. I was thinking I could use some combination of $cond and $size to check explicitly for a size of zero within another $project, and then substitute the same literal in that case, but $if expects a boolean so I'm not quite sure how to format that.

推荐答案

修改你的项目阶段管道如下:

Modify your project stage in the pipeline as below:

投影阶段做了以下两件事:

The projection stage does the following two things:


  • 如果items数组 null 不存在 ,计算
    项目字段值为 [] (一个空数组。)

  • 接下来检查<$ c刚刚计算的项目字段的$ c> size 是 0 ,如果
    它是 0 ,然后将其值更改为具有默认对象的数组。

  • If the items array is null or does not exist, calculates the items field value as [](an empty array.)
  • Next checks if the size of the just calculated items field is 0, if it is 0, then changes its value to an array with default object.

阶段代码:

db.MyCollection.aggregate([
...
{$project:{"_id":"$$ROOT",
           "items":{$cond:[
                          {$eq:[{$size:{$ifNull:["$items",[]]}},0]},
                          [{"quantity":0,"price":0}],
                          "$items"
                          ]}}},
{$unwind:"$items"},
...
])

这篇关于如何在Mongo的聚合管道的$ unwind阶段保留零长度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆