I have data that has user primary keys in "user.pk", and "usertags.user.pk".
[
{
_id: ObjectId("62015250cb18d3cadb32c38e"),
user: { pk: '7485070525' },
usertags: [ { user: { pk: '334777488' } } ]
},
{
_id: ObjectId("62015250cb18d3cadb32c38f"),
user: { pk: '334777488' },
usertags: [
{ user: { pk: '7485070525' } },
{ user: { pk: '271738610' } },
{ user: { pk: '31961021' } }
]
}
]
I am looking to aggregate all this data into a single list of all distinct user primary keys, so the result for the data above would be something like { _id: null, user_pks: [7485070525, 334777488, 271738610, 31961021] }
I tried using the $group aggregation, but I can't figure out how to merge "user.pk", and "usertags.user.pk". Can anyone help me with that?
Edit: This is the closest I've gotten, which puts users and usertags in separate lists.
db.collection.aggregate([
{ $unwind: "$usertags" },
{ $group: { _id: null, users: { $push: "$user.pk" }, usertags: {$push: "$usertags.user.pk" } } },
])