I have written a MongoDB query that looks like this:
db.collection.aggregate([
{
$group: {
_id: null,
categories: {
$addToSet: "$category"
}
}
},
{
$project: {
_id: 0,
categories: 1
}
}
])
but the result is:
[
{
"categories": [
"Enhancement",
"Bug"
]
}
]
how can I get a result like this:
["Enhancement", "Bug"]
2
Answers
You can maybe try to modify the query to include an additional aggregation stage using
$unwind
and$replaceRoot
operators. Tell me if it works for you.MongoDB is a document database, it will always return a document or an array of documents. There is no query method in MongoDB that will return a scalar value or an array of scalar values.
You can do modification on the client side. If you are using javascript, you could get that result with: