I'm trying to do a find which will get all documents which has a title or desc that contains the regular expression (which works) and then filter it further to only contain documents which either have the "private" flag set to false or the "user" field set to the given userId.
Here's what I'm trying so far...
FCSet.find({}, {"flashCards":0}).or([{ 'title': { $regex: re }}, { 'desc': { $regex: re }}]).or([{ 'private': false}, { 'user': 'userId'}]).sort('title')
This should only be returning 6 rows but it's returning 56. If I take out the 2nd or() then it works for filtering by title/desc. I'm guessing that I'm going about it the wrong way.
Can someone help me figure out how to do this correctly?
You have to put the OR
s into an AND
:
FCSet.find(
{ "flashCards": 0 },
$and: [
{
$or: [
{ 'title': { $regex: re } },
{ 'desc': { $regex: re } }
]
},
{
$or: [
{ 'private': false },
{ 'user': 'userId' }
]
}
]
).sort('title')
The answer by @heinob is totally correct however I implore you not to use it.
The biggest reason is that nested $or
queries DO NOT USE INDEXES: https://jira.mongodb.org/browse/SERVER-3327
(in fact more specific to your case: https://jira.mongodb.org/browse/SERVER-6542 )
As such any index on that block will be utterly useless.
Instead you would be better off coding into your application a means to create a single $or
block instead of doing what looks like the easiest way.
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