I am struggling to find a good way of executing a compound MongoDB find() with the following criteria:
In my particular case, I have a collection of users which contains a list of users and their associated permissions. It also contains a special entry for a guest pseudo-user that contain default permissions. This guest document is the one that needs to be returned if an exact user match does not take place.
I have tried using {$or: [ { id: "bob" }, {id: "guest" } ]} as well as { id: { $in: [ "bob", "guest" ] } }, but both of these search the collection sequentially and would return either "bob" or "guest" depending on which was inserted first.
Is there a way to explicitly say find bob or as a fallback, return the guest document?
Keep in mind this would be executed from Node.js, hence I'm looking for a clean & simple way to offload this to MongoDB.
I realize this can be done as two queries, but I would like to avoid that scenario if possible.
Thank you!
if you insert guest as the first object, then by combination of limit and sort by _id you can solve this!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With