Manthan
Manthan

Reputation: 343

MongoDB Find Query, dynamically change search keys based on key-value in a document

Country data looks like -

[{
   inEU : true,
   x    : {...},
   y    : {...}
}
{
   inEU : false,
   x    : {...},
   y    : {...}
}]

I need to choose either x or y object based on inEU field is set to true or false. X and Y object have the same keys (values/data is different).

Find query must use values from X if inEU is true else use Y object for search purpose.

Kindly let me know if its possible and if yes do provide any URL/links of documentation or any small example will help me.

Upvotes: 0

Views: 137

Answers (1)

Cuong Le Ngoc
Cuong Le Ngoc

Reputation: 11975

You can use $or to do that. For example:

Model.find( { $or: [ { inEU: true, x: condition }, { inEU: false, y: condition } ] } )...

Upvotes: 0

Related Questions