MogoDB subdocument filteration - mongodb

{
"_id" : ObjectId("5a4e43edb85ed11cd4dcba45"),
"email" : "av#gmail.com",
"username" : "alpesh",
"subscriptions" : [
{
"sub_id" : "5a4df654b9799b79147f9361",
"activation_date" : ISODate("2017-12-19T18:30:00.000Z"),
"expiry_date" : ISODate("2018-01-19T18:30:00.000Z")
},
{
"sub_id" : "5a4df654b9799b79147f9361",
"activation_date" : ISODate("2018-01-19T18:30:00.000Z"),
"expiry_date" : ISODate("2018-02-19T18:30:00.000Z")
},
{
"sub_id" : "5a51a925ddc5003b68cc38b3",
"activation_date" : ISODate("2018-02-19T18:30:00.000Z"),
"expiry_date" : ISODate("2018-03-22T18:30:00.000Z")
}
]
}
i have tried this..
db.find({"subscriptions.sub_id" : "5a4df654b9799b79147f9361" });
it returns ..
{
"_id" : ObjectId("5a4e43edb85ed11cd4dcba45"),
"email" : "av#gmail.com",
"username" : "alpesh",
"subscriptions" : [
{
"sub_id" : "5a4df654b9799b79147f9361",
"activation_date" : ISODate("2017-12-19T18:30:00.000Z"),
"expiry_date" : ISODate("2018-01-19T18:30:00.000Z")
},
{
"sub_id" : "5a4df654b9799b79147f9361",
"activation_date" : ISODate("2018-01-19T18:30:00.000Z"),
"expiry_date" : ISODate("2018-02-19T18:30:00.000Z")
},
{
"sub_id" : "5a51a925ddc5003b68cc38b3",
"activation_date" : ISODate("2018-02-19T18:30:00.000Z"),
"expiry_date" : ISODate("2018-03-22T18:30:00.000Z")
}
]
}
i have also tried $aggregate , $unwind , $filter , $projection and many ways but none of them returns as expected...
i want all the matching subdocuments...like this....
{
"_id" : ObjectId("5a4e43edb85ed11cd4dcba45"),
"email" : "av#gmail.com",
"username" : "alpesh",
"subscriptions" : [
{
"sub_id" : "5a4df654b9799b79147f9361",
"activation_date" : ISODate("2017-12-19T18:30:00.000Z"),
"expiry_date" : ISODate("2018-01-19T18:30:00.000Z")
},
{
"sub_id" : "5a4df654b9799b79147f9361",
"activation_date" : ISODate("2018-01-19T18:30:00.000Z"),
"expiry_date" : ISODate("2018-02-19T18:30:00.000Z")
}
]
}

db.collection('gyms').aggregate([
{
$match: {
subscriptions: {
$elemMatch: { sub_id: "5a4df654b9799b79147f9361" }
}
}
},
{
$redact: {
$cond: {
if: {
$or: [
{ $eq: ["$sub_id", "5a4df654b9799b79147f9361" ] },
{ $not: "$sub_id" }
]
},
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
])
this is generating as expected within a single doucment without unnecessary sub documents.
{
"email": "av#gmail.com",
"username": "alpesh",
"subscriptions": [
{
"sub_id": "5a4df654b9799b79147f9361",
"activation_date": "2017-12-19T18:30:00.000Z",
"expiry_date": "2018-01-19T18:30:00.000Z"
},
{
"sub_id": "5a4df654b9799b79147f9361",
"activation_date": "2018-01-19T18:30:00.000Z",
"expiry_date": "2018-02-19T18:30:00.000Z"
}
]
}

Two ways to achieve that. You can use $elemMatch as a projection operator but this will only bring you first matching subdocument:
db.collection.find(
{ "subscriptions.sub_id": "5a4df654b9799b79147f9361" },
{ "subscriptions" : { $elemMatch: { "sub_id": "5a4df654b9799b79147f9361" } } }
)
Second way is by using aggregation framework:
db.collection.aggregate([
{ $unwind: "$subscriptions" },
{ $match: {"subscriptions.sub_id": "5a4df654b9799b79147f9361"} },
{
$group: {
_id: "$_id",
"email" : { $first: "$email" },
"username" : { $first: "$username" },
"subscriptions": { $push: "$subscriptions" }
}
}
])
Here the point is that you need to group back your unwinded subscriptions to get back original shape (after filtering). To retrieve accumulate subscriptions into array you can use $push operator and to get back properties from outer document you can move them from any matching document (by using $first or $last).

Related

Merge two documents and reshape with arrays

Input data
[
{
"_id" : ObjectId("xxx"),
"ParentNumber" : "12345",
"ChildNumber" : "A123"
},
{
"_id" : ObjectId("yyy"),
"ParentNumber" : "12345",
"ChildNumber" : "B123"
},
{
"_id" : ObjectId("zzz"),
"ParentNumber" : "6789",
"ChildNumber" : "C123"
}
]
Output Needed
[
{
"_id" : ObjectId("aaa"),
"ParentNumber" : "12345",
"Children":[
{ "ChildNumber" : "A123"},
{ "ChildNumber" : "B123"}
]
},
{
"_id" : ObjectId("bbb"),
"ParentNumber" : "6789",
"Children":[
{ "ChildNumber" : "C123"}
]
}
]
I tried the following but can't figure out how to group the parent numbers with children.
db.test.aggregate
(
[
{
$project:
{
"_id" : ObjectId(),
"ParentNumber" : "$ParentNumber",
"Children" : [
{
"ChildNumber" : "$ChildNumber"
}
]
}
}
]
)
I referred to the merge function in mongodb but I can't figure out how to compare one document to another based on a condition and return a result with array.
Thank you
This gives the desired result. Try this:
[{$group: {
_id: "$ParentNumber",
children: {
$push:{'ChildNumber':'$ChildNumber'}
},
}}, {$project: {
"ParentNumber":"$_id",
children:1
}}]
Check this PS: it will not show the objectId of the parent
db.collection.aggregate([
{
$group: {
_id: "$ParentNumber",
Children: {
$push: "$$ROOT"
},
},
},
{
"$project": {
_id: 0,
ParentNumber: "$_id",
Children: {
"ChildNumber": "$Children.ChildNumber"
}
}
}
])

How to compare nested array elements with each other and count the total sub documents?

my mongodb document set look like this
{
"_id" : ObjectId("59093a8e1104a53169"),
"createdAt" : ISODate("2017-05-03T02:03:58.249+0000"),
"phone" : "0000000000",
"email" : "abc#gmail.com",
"dob" : "12/26/1976",
"password" : "*******",
"stripeID" : "***",
"picture" : "htt://g",
"name" : {
"first" : "P",
"last" : "e"
},
"addresses" : [
{
"description" : "237 S ABCD, USA",
"_id" : ObjectId("59093bsaaudua"),
"loc" : [
-008.2478742,
124.0517012
]
},
{
"apartment" : "",
"description" : "787 S Defghsvd USA",
"_id" : ObjectId("5a26b77dfhgswj"),
"loc" : [
-18.01,
34.039058
]
},
{
"description" : "13210 hdsg sdjhf 90284, USA",
"_id" : ObjectId("5d2482basasas17be1"),
"loc" : [
-18.01,
-18.01
]
}
]
}
what i need to do is compare loc[0] with loc[1] if addresses exists in the document and know how many of them has this x === y. i don't know how to approach this. any help would be great. thanks in advance.
i.e. what i want is in all the documents if any user has equal loc array element's, then i want to find those documents. my query should return like:
{
"description" : "13210 hdsg sdjhf 90284, USA",
"_id" : ObjectId("5d2482basasas17be1"),
"loc" : [
-18.01,
-18.01
]
}
this should do the trick:
db.collection.aggregate([
{
$unwind: '$addresses'
},
{
$match: {
$expr: {
$eq: [
{ $arrayElemAt: ["$addresses.loc", 0] },
{ $arrayElemAt: ["$addresses.loc", 1] }
]
}
}
},
{
$replaceRoot: {
newRoot: "$addresses"
}
}
])
https://mongoplayground.net/p/YRnbPm-qfe6
if you also want the count, you can do this:
db.collection.aggregate([
{
$unwind: '$addresses'
},
{
$match: {
$expr: {
$eq: [
{ $arrayElemAt: ["$addresses.loc", 0] },
{ $arrayElemAt: ["$addresses.loc", 1] }
]
}
}
},
{
$replaceRoot: {
newRoot: "$addresses"
}
},
{
$group: {
_id: null,
count: {
$sum: 1
},
addresses: {
$push: '$$ROOT'
}
}
},
{
$project: {
_id: 0
}
}
])
https://mongoplayground.net/p/Kqi4J7f-4go

MongoDB Keep path where a criteria is met

I'm new to MongoDB.
In the find query I'm using the following structure:
db.report.find({'accountList.transactionList.description': /.*aear.*/i})
However, accountList contains multiple values, and so does transaction list, the exact query would be:
db.report.find({'accountList[0].transactionList[4].description': /.*aear.*/i})
The problem is that accountList has multiple accounts, and only one of them has the value 'aear' in the description. When I'm executing the query it returns me both accounts, and I'd like to keep only the account where aear is in its description. Also, this MUST be iterable over many files, since it file has different transactionLists, therefore in some documents aear will not appear at all, and in others it might appear multiple types, always in different positions. I believe something must be done in projection, but setting it like this doesn't work:
.projection({"accountList.id":1,"accountList.transactionList.description":1})
Here's the output:
"accountList" : [
{
"id" : "1",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
"description" : "aear"
},
{
"onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
"description" : "bb"
},
{
"onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
"description" : "cc"
}
]
},
{
"id" : "2",
"type" : "xD",
"currency" : "USD",
"transactionList" : [
{
"onDate" : ISODate("2019-08-15T21:00:00.000-03:00"),
"description" : "aa",
},
{
"onDate" : ISODate("2019-08-14T21:00:00.000-03:00"),
"description" : "ee"
}
]
}
]
And I'd like something like this, where I''m only getting the path to where the condition is met:
"accountList" : [
{
"id" : "1",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
"description" : "aear"
},
To accomplish that you need to use aggregate. I believe this code will work in your case:
db.report.aggregate([
{ "$match": { "accountList.transactionList.description": { $regex: "aear", $options: "i"} } },
{ "$unwind": "$accountList" },
{ "$unwind": "$accountList.transactionList" },
{ "$match": { "accountList.transactionList.description": { $regex: "aear", $options: "i"} } },
{ "$group": {
"_id": {
"_id": "$_id",
"accountListId": "$accountList.id",
"accountListType": "$accountList.type",
"accountListCurrency": "$accountList.currency",
},
"transactionList": { "$push": "$accountList.transactionList" }
}},
{ "$group": {
"_id": "$_id._id",
"accountList": {
"$push": {
"id": "$_id.accountListId",
"type": "$_id.accountListType",
"currency": "$_id.accountListCurrency",
"transactionList": "$transactionList"
}
}
}}
])
Updating my answer as this question got updated with new required o/p :
Answer for New Question :
If you've only one transaction matching to given criteria /.*aear.*/i, let's say description is unique across accountList array of report document(exact for provided sample):
db.report.aggregate([{
$match: {
'accountList.transactionList.description': /.*aear.*/i
}
},{ $unwind: '$accountList' },{ $unwind: '$accountList.transactionList' },{$match :{ 'accountList.transactionList.description': /.*aear.*/i}}, { $project: { 'accountList': 1, _id: 0 } }])
But, if you've multiple descriptions (across multiple objects in accountsList array of a report document) matches to given criteria in accountList :
db.report.aggregate([{
$match: {
'accountList.transactionList.description': /.*aear.*/i
}
}, { $unwind: '$accountList' }, { $unwind: '$accountList.transactionList' }, { $match: { 'accountList.transactionList.description': /.*aear.*/i } },
{ $group: { _id: '$_id', accountList: { $push: '$accountList' }, data: { $first: '$$ROOT' } } }
, { $addFields: { 'data.accountList': '$accountList' } }, { $replaceRoot: { 'newRoot': '$data' } }, { $project: { 'accountList': 1, _id: 0 } }
])
Output :
/* 1 */
{
"accountList" : [
{
"id" : "1100",
"type" : "xD",
"currency" : "EUR",
"transactionList" : {
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
},
{
"id" : "1200",
"type" : "xD",
"currency" : "USD",
"transactionList" : {
"onDate" : ISODate("2019-08-16T00:00:00.000Z"),
"description" : "aear"
}
}
]
}
/* 2 */
{
"accountList" : [
{
"id" : "1",
"type" : "xD",
"currency" : "EUR",
"transactionList" : {
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
}
]
}
If in case you've multiple matching descriptions in transaction array & also in other objects of accounts array (this will work for all above scenarios as well but it might not be needed as per requirement, it can be bulky, Check document#3 in Output for clarification) :
db.report.aggregate([
{ "$match": { "accountList.transactionList.description": /.*aear.*/i } },
{ "$unwind": "$accountList" },
{ "$unwind": "$accountList.transactionList" },
{ "$match": { "accountList.transactionList.description": /.*aear.*/i } },
{
"$group": {
"_id": {
"docId": "$_id",
"accountsListObjId": "$accountList.id"
},
"transactionList": { "$push": "$accountList.transactionList" },
"accountList": { "$first": '$accountList' }
}
}
, { $addFields: { 'accountList.transactionList': '$transactionList' } },
{
"$group": {
"_id": "$_id.docId",
"accountList": { $push: '$accountList' }
}
}, { $project: { 'accountList': 1, _id: 0 } }
])
Output :
/* 1 */
{
"accountList" : [
{
"id" : "1100",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
]
},
{
"id" : "1200",
"type" : "xD",
"currency" : "USD",
"transactionList" : [
{
"onDate" : ISODate("2019-08-16T00:00:00.000Z"),
"description" : "aear"
}
]
}
]
}
/* 2 */
{
"accountList" : [
{
"id" : "1",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
]
}
]
}
/* 3 */
{
"accountList" : [
{
"id" : "00",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
},
{
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
]
},
{
"id" : "100",
"type" : "xD",
"currency" : "USD",
"transactionList" : [
{
"onDate" : ISODate("2019-08-16T00:00:00.000Z"),
"description" : "aear"
}
]
}
]
}
If you're looking for exact text, you can do this as well(cause regex is not allowed in cond) :
db.report.aggregate([
{
$match: {
'accountList.transactionList.description': 'aear'
}
}, { $unwind: '$accountList' }, {
$addFields: {
'accountList.transactionList': {
$filter: {
input: '$accountList.transactionList',
as: 'eachTransaction',
cond: { $eq: ["$$eachTransaction.description", 'aear'] }
}
}
}
}, { $match: { 'accountList.transactionList': { $ne: [] } } }, { $group: { _id: '$_id', accountList: { $push: '$accountList' }, data: { $first: '$$ROOT' } } }
, { $addFields: { 'data.accountList': '$accountList' } }, { $replaceRoot: { 'newRoot': '$data' } }, { $project: { 'accountList': 1, _id: 0 } }])
Output : Same as above.
Answer for Old Question :
Ok you've two options here, Please try these :
If you've only one object in accountList which does matches with the given filter then you can simply do this:
db.report.find({'accountList.transactionList.description': /.*aear.*/i}, {'accountList.$': 1})
Output :
/* 1 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
"accountList" : [
{
"id" : "4474",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
"accountList" : [
{
"id" : "4400",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 3 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df077"),
"accountList" : [
{
"id" : "0000",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 4 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
"accountList" : [
{
"id" : "0101",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
Downside of above .find () query is it would get only first matching object in accountList, If you've multiple matching objects for given filter in accountList then you need to use aggregation (this aggregation query can be used for earlier scenario as well, Please check output for diff) :
db.report.aggregate([
{
$match: {
"accountList.transactionList.description": /.*aear.*/i
}
},
{ $unwind: "$accountList" },
{
$match: {
"accountList.transactionList.description": /.*aear.*/i
}
}, { $group: { _id: '$_id', accountList: { $push: '$accountList' }, doc: { $first: '$$ROOT' } } }, { $addFields: { 'doc.accountList': '$accountList' } },
{ $replaceRoot: { 'newRoot': '$doc' } }
])
Output :
// This first object is best example where you need aggregation
/* 1 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
"accountList" : [
{
"id" : "0101",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
},
{
"id" : "1111",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
"accountList" : [
{
"id" : "4400",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 3 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df077"),
"accountList" : [
{
"id" : "0000",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 4 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
"accountList" : [
{
"id" : "4474",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
Try this query:
db.report.find({'accountList[0].transactionList[4].description': { $regex: /.*aear.*/i} })
OR - Which will return only the first matching document:
db.report.find({'accountList[0].transactionList[4].description': /.*aear.*/i}).limit(1)

mongodb aggregation with array and lookup

I have a MongoDB collection with documents in the following format
collection name called post
{
"_id" : ObjectId("5c88b225fac24431d947abad"),
"user_id" : "5c87ad6c623f1e2bd4d041d0",
"post_likes" : [
{
"post_user_id" : "5c87ad6c623f1e2bd4d041d0",
"like_status" : true
},
{
"post_user_id" : "5c88b42b71611926c055508b",
"like_status" : true
}
],
"post_comments" : [
{
"comment_user_id" : "5c87ad6c623f1e2bd4d041d0",
"comment_like" : "",
"comment_description" : ""
},
{
"comment_user_id" : "5c88b42b71611926c055508b",
"comment_like" : "",
"comment_description" : "nice post"
}
]
}
i have another collection name called user_ptofile_info
{
"_id" : ObjectId("5c923682c088564cf01056cb"),
"user_id" : "5c87ad6c623f1e2bd4d041d0",
"image_url" : "image/url",
"user_name":"xxxxxxxx",
"created_at" : "",
"updated_at" : ""
}
requested output like
note:the post_user_id from user_ptofile_info and user_id from post are lookup and i need total count for post_likes and post comments also
{
"_id" : ObjectId("5c88b225fac24431d947abad"),
"user_id" : "5c87ad6c623f1e2bd4d041d0",
"post_likes" : [
{
"post_user_id" : "5c87ad6c623f1e2bd4d041d0",
"like_status" : true,
"image_url" : "image/url",
"user_name":"xxxxxxxx",
},
{
"post_user_id" : "5c88b42b71611926c055508b",
"like_status" : true,
"image_url" : "image/url",
"user_name":"xxxxxxxx",
}
],
"post_comments" : [
{
"comment_user_id" : "5c87ad6c623f1e2bd4d041d0",
"comment_like" : "",
"comment_description" : ""
},
{
"comment_user_id" : "5c88b42b71611926c055508b",
"comment_like" : "",
"comment_description" : "nice post"
}
]
}
You can use below aggregation:
db.post.aggregate([
{
$lookup: {
from: "user_profile_info",
let: { user_ids: "$post_likes.post_user_id" },
pipeline: [
{ $match: { $expr: { $in: [ "$user_id", "$$user_ids" ] } } },
{
$project: {
post_user_id: "$user_id",
image_url: 1,
user_name: 1
}
}
],
as: "users"
}
},
{
$project: {
_id: 1,
user_id: 1,
post_likes: {
$map: {
input: "$users",
as: "user",
in: {
post_user_id: "$$user.post_user_id",
image_url: "$$user.image_url",
user_name: "$$user.user_name",
like_status: {
$let: {
vars: {
like: {
$arrayElemAt: [
{ $filter: { input: "$post_likes", as: "pl", cond: { $eq: [ "$$pl.post_user_id", "$$user.post_user_id" ] } } }, 0
]
}
},
in: "$$like.like_status"
}
}
}
}
},
post_comments: 1
}
}
])
$lookup with custom pipeline (MongoDB 3.6 or newer) will allow you to get the data from user_profile_info for all users that are present in post_likes array. Then you need to "merge" users array with post_likes to get like_status. Since you have two arrays and you know that the same post_user_id appears in both of them you can use $map with $arrayElemAt and $filter to combine the data from both arrays.

Issue retrieving subdocuments from MongoDB

I have the following dataset:
{
"_id" : ObjectId("59668a22734d1d48cf34de08"),
"name" : "Nobody Cares",
"menus" : [
{
"_id" : "menu_123",
"name" : "Weekend Menu",
"description" : "A menu for the weekend",
"groups" : [
{
"name" : "Spirits",
"has_mixers" : true,
"sizes" : [
"Single",
"Double"
],
"categories" : [
{
"name" : "Vodka",
"description" : "Maybe not necessary?",
"drinks" : [
{
"_id" : "drink_123",
"name" : "Absolut",
"description" : "Fancy ass vodka",
"sizes" : [
{
"_id" : "size_123",
"size" : "Single",
"price" : 300
}
]
}
]
}
]
}
],
"mixers" : [
{
"_id" : "mixer_1",
"name" : "Coca Cola",
"price" : 150
},
{
"_id" : "mixer_2",
"name" : "Lemonade",
"price" : 120
}
]
}
]
}
And I'm attempting to retrieve a single drink from that dataset, I'm using the following aggregate query:
db.getCollection('places').aggregate([
{ $match : {"menus.groups.categories.drinks._id" : "drink_123"} },
{ $unwind: "$menus" },
{ $project: { "_id": 1, "menus": { "groups": { "categories": { "drinks": { "name": 1 } } } } } }
])
However, it's returning the full structure of the dataset along with the correct data.
So instead of:
{
"_id": "drink_123",
"name": "Absolut"
}
I get:
{
"_id": ObjectId("59668a22734d1d48cf34de08"),
"menus": {
"groups": {
"categories": {
"drinks": { "name": "Absolut" }
}
}
}
}
For example. Any ideas how to just retrieve the subdocument?
If you need to retain the deeply nested model then this call will produce the desired output:
db.getCollection('places').aggregate([
{ $match : {"menus.groups.categories.drinks._id" : "drink_123"} },
{ $project: {"_id": '$menus.groups.categories.drinks._id', name: '$menus.groups.categories.drinks.name'}},
{ $unwind: "$name" },
{ $unwind: "$name" },
{ $unwind: "$name" },
{ $unwind: "$name" },
{ $unwind: "$_id" },
{ $unwind: "$_id" },
{ $unwind: "$_id" },
{ $unwind: "$_id" }
])
The numerous unwinds are the result of the deep nesting of the drinks subdocuments.
Though, FWIW, this sort of query does perhaps suggest that the model isn't 'read friendly'.