How do I limit the match to have only 1
I don't think there is better way to limit document in match stage using $in
query.
My issue is that this is quite slow as it matches
You need to avoid such array operations from your query,
- remove last
$project
stage and do that operation in your client side,
- create single field index on
node
field, it will be more effective for performance,
- i am suggesting below query, i have not tested it but you can try may be it will help,
Suggestion 1:
{ $match: { node: { $in: ["a", "b", "c"] } } },
{ $group: { "_id": "$node" } }
Playground
Result:
[{ "_id": "c" }, { "_id": "a" }]
Suggestion 2:
{ $match: { node: { $in: ["a", "b", "c"] } } },
{ $group: { "_id": null, "matches": { $addToSet: "$node" } } }
Playground
Result:
[{ "_id": null, "matches": ["c", "a"] }]
Other stuff you can do in your client side code.
You can check state of your query using explain(), see the examples and you can try in your query like db.docs.explain().aggregate(..)
it will provide each statistics of your query stages.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…