CrazySabbath
CrazySabbath

Reputation: 1334

MongoDB OR not in a top level

I'm trying to build a query like this:

"i_field": {
    "$or": [{
            query_1
        }, {
            query_2
        }
    ]
}

However mongo throws exception "Can't canonicalize query: BadValue unknown operator: $or" since I must use $or in top level (or that's my understanding so far).

Working OR in top level query: (but that's not what I need)

"$or": [{
        "i_field": query_1
    }, {
        "i_field": query_2
    }
]

Does anyone have any suggestions how could I make this query work?

Edit (real exmple):

"i_field": {
    "$or": [{
            "$gte": 3.5,
            "$lt": 88.5
        }, {
            "$eq": null
        }
    ]
}

Upvotes: 0

Views: 233

Answers (2)

Joe
Joe

Reputation: 28326

Putting $or in the top level is the way to go:

"$or":[
   { "i_field":{"$gte": 3.5, "$lt": 88.5}}, 
   { "i_field": null}
]

Playground

Upvotes: 1

Raymond Reddington
Raymond Reddington

Reputation: 1837

Use $in instead:

{ "i_field": { "$in": [query_1, query_2] } }

Upvotes: 0

Related Questions