Reputation: 1182
I have an mongoDB objetc like :
[
{
"items": [
{
"title": "hello",
"options": [
{
"value": "A",
"image": "img1",
"des": "des1"
},
{
"value": "B",
"image": "img2",
"des": "des2"
},
]
}
]
}
]
When I retrieve data from the collection, I want to retrieve only value
key from the options
of the items
array.
Output should look like :
{
"items":[
{
"options" :[
{ "value" :"A" },
{ "value" :"B" },
]
}
]
}
How can i do this?
Upvotes: 1
Views: 48
Reputation: 20354
You can do it like this:
db.collection.find({}, { "items.options.value": 1})
Here is the working example: https://mongoplayground.net/p/FqDaQ7Q-y-Y
Or you can do it like this:
db.collection.aggregate([
{
"$project": {
"items.options.value": 1
}
}
])
Here is the working example: https://mongoplayground.net/p/jPFYtXYBvpI
Upvotes: 1