An empty array prevents a document from appearing in a request

I have documents with multiple fields, and in particular, there is a field called attrs, which is an array. I use the aggregation pipeline.

In my request, I am interested in the attrs (attributes) field if it has any elements in it. Otherwise, I still want to get the result. In this case, I follow the type of the document field.

The problem is that if the document does not contain any element in the attrs field, it will be filtered out and I will not get its _id.type field, which I really want from this request.

{
    aggregate: "entities",
    pipeline: [
        {
            $match: {
                _id.servicePath: {
                    $in: [
                        /^/.*/,
                        null
                    ]
                }
            }
        },
        {
            $project: {
                _id: 1,
                "attrs.name": 1,
                "attrs.type": 1
            }
        },
        {
            $unwind: "$attrs"
        },
        {
            $group: {
                _id: "$_id.type",
                attrs: {
                    $addToSet: "$attrs"
                }
            }
        },
        {
            $sort: {
                _id: 1
            }
        }
    ]
}

So, the question arises: how can I get a result containing all types of documents, regardless of attrs, but including attributes if they exist?

Hope this makes sense.

+4
3

$cond $project, attr , , null, , , attr.

, $project, $unwind:

    {
        $project: {
            attrs: {$cond: {
               if: {$eq: ['$attrs', [] ]},
               then: [null],
               else: '$attrs'
           }}
        }
    },

, null attrs , - attrs, , .

$match, , .

[
  {_id: {type: 1, id: 2}, attrs: []},
  {_id: {type: 2, id: 1}, attrs: []},
  {_id: {type: 2, id: 2}, attrs: [{name: 'john', type: 22}, {name: 'bob', type: 44}]}
]

{
    "result" : [ 
        {
            "_id" : 1,
            "attrs" : [ 
                null
            ]
        }, 
        {
            "_id" : 2,
            "attrs" : [ 
                {
                    "name" : "bob",
                    "type" : 44
                }, 
                {
                    "name" : "john",
                    "type" : 22
                }, 
                null
            ]
        }
    ],
    "ok" : 1
}

db.test.aggregate([
    {
        $match: {
            '_id.servicePath': {
                $in: [
                    null
                ]
            }
        }
    },
    {
        $project: {
            _id: 1,
            "attrs.name": 1,
            "attrs.type": 1
        }
    },
    {
        $project: {
            attrs: {$cond: {
               if: {$eq: ['$attrs', [] ]},
               then: [null],
               else: '$attrs'
           }}
        }
    },
    {
        $unwind: "$attrs"
    },
    {
        $group: {
            _id: "$_id.type",
            attrs: {
                $addToSet: "$attrs"
            }
        }
    },
    {
        $sort: {
            _id: 1
        }
    }
])
+4

if .

-, .

, 0, . , .

, , , .

-1

$ ' : attr, , , attr null, attr ( $exists)

-1
source

All Articles