Reputation: 356
I want to convert this array of object
[{ category:"AAA" },{ category:"BBB" },{ category: "CCC" }]
Into this
["AAA","BBB","CCC"]
. I don't want to filter or use any array function in the backend but from the mongoDB itself.
Upvotes: 0
Views: 74
Reputation: 9652
$map maps the object keys in the array to an array of the key values. Then use $addFields to transfrom the output
arr = [{ category:"AAA" },{ category:"BBB" },{ category: "CCC" }];
db.collection.aggregate([
{
"$addFields": {
"exclude": {
"$map": {
"input": "$arr",
"as": "el",
"in": "$$el.category"
}
}
}
}
])
Upvotes: 0
Reputation: 6714
db.collection.distinct('category')
should give you an array of unique values for that field.
Upvotes: 2