Answer the question
In order to leave comments, you need to log in
Is it possible to simplify the query when fetching an array of ids?
I need to get an array of identifiers where the user is mentioned, I do it like this
return Collection.aggregate([
{
$match: {
likes: userId
}
}, {
$group: {
_id: null,
ids: {
$addToSet: '$_id'
}
}
}]).then(e => {
return e[0].ids
})
Answer the question
In order to leave comments, you need to log in
You can write something along the lines of:
const [{ ids: idsArray }] = await Collection.aggregate([
{
$match: {
likes: userId
}
}, {
$group: {
_id: null,
ids: {
$addToSet: '$_id'
}
}
}]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question