0

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.

1
  • Did you try something like this: Group.find({program: {$in: [...]}}).lean().select('category') Commented Mar 5, 2020 at 10:09

2 Answers 2

2

db.collection.distinct('category')

should give you an array of unique values for that field.

Sign up to request clarification or add additional context in comments.

Comments

0

$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"
                }
            }
        }
    }
])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.