{
"_id" : ObjectId("5a4e43edb85ed11cd4dcba45"),
"email" : "[email protected]",
"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" : "[email protected]",
"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" : "[email protected]",
"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": "[email protected]",
"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"
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With