0
Follow
2
View

MongoDB $lookup and $group returns empty Array

yazippx 注册会员
2023-01-25 02:50

You're trying to use the id field for the $lookup but after this $group stage:

{ $group: { _id: '$_id', names: { $push: { value: '$names.value', changed_at: '$names.changed_at' } } } },

The document structure is:

{
  _id: ObjectId,
  names: { value: string, changed_at: Date }[]
}

As you can see, there is no id field.

If this is a typo just change id to _id. if this is not a typo then you need to "save" the id field in the $group stage, like so:

{
    $group: {
        _id: '$_id',
        names: {$push: {value: '$names.value', changed_at: '$names.changed_at'}},
        id: {$first: "$id"}
    }
}

About the Author

Question Info

Publish Time
2023-01-25 02:50
Update Time
2023-01-25 02:50