'Output a specific result from array of objects aggregation - MongoDB
I have a data looking like this:
{
"_id": ObjectId("620ca4e292a990273446cf7b"),
"data": [
{
"flagging": "BUSINESS",
"information": [
{
"room": 1,
"status": "AVAILABLE"
},
{
"room": 2,
"status": "BUSY"
}
]
},
{
"flagging": "PRIVATE",
"information": [
{
"room": 1,
"status": "AVAILABLE"
},
{
"room": 2,
"status": "CLEANING"
}
]
},
{
"flagging": "PUBLIC",
"information": [
{
"room": 1,
"status": "SERVICE"
},
{
"room": 2,
"status": "AVAILABLE"
}
]
}
],
"createdAt": "2022-02-16T07:16:50.674Z"
}
I'm trying to make a service whether user can input a certain flagging, and the data with the corresponding flagging will show as a result. I tried using this code below but what I get are all the datas, not the filtered ones.
db.coll.aggregate([
{
$match: { _id: ObjectId("620ca4e292a990273446cf7b") },
},
{
$project: { _id: 0, "data": 1 }
}
])
What should I change in the code so I can get the following result (if the requested flagging are BUSINESS and PUBLIC)?
{
"data": [
{
"flagging": "BUSINESS",
"information": [
{
"room": 1,
"status": "AVAILABLE"
},
{
"room": 2,
"status": "BUSY"
}
]
},
{
"flagging": "PUBLIC",
"information": [
{
"room": 1,
"status": "SERVICE"
},
{
"room": 2,
"status": "AVAILABLE"
}
]
}
]
}
Thanks for the help 🙏🏻
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|