Reputation: 318
My document:
[
{
"_id": "5f969419d40c1580f2d4aa36",
"users": {
"[email protected]": "baz",
"[email protected]": "baz2"
}
},
{
"_id": "5f9694d4d40c1580f2d4aa38",
"users": {
"[email protected]": "foo"
}
}
]
If i use this aggregate, i get two users. Ok. But how can i get only the value of "[email protected]"? Test in https://mongoplayground.net/p/3kW2Rw6fSjh
db.collection.aggregate([
{
"$project": {
"users": {
"$objectToArray": "$users"
}
}
},
{
"$match": {
"users.k": "[email protected]"
}
},
{
"$project": {
"users": {
"$arrayToObject": "$users"
}
}
}
])
Upvotes: 1
Views: 41
Reputation: 16033
You can add a $filter
stage after the $match
stage:
{
$set: {
users: {
$filter: {
input: "$users",
cond: {
$eq: [
"$$this.k",
"[email protected]"
]
}
}
}
}
},
See how it works on the playground example
Upvotes: 1