I am using the MongoDB PHP Library and have the following query array
Array
(
[$and] => Array
(
[0] => Array
(
[$or] => Array
(
[0] => Array
(
[role.name] => Array
(
[$in] => Array
(
[0] => 'User'
[1] => 'VIP User'
)
)
)
[1] => Array
(
[role._id] => Array
(
[$in] => Array
(
[0] => 'User'
[1] => 'VIP User'
)
)
)
)
)
)
)
Which does not work. However, this does:
Array
(
[$and] => Array
(
[0] => Array
(
[role.name] => Array
(
[$in] => Array
(
[0] => 'User'
[1] => 'VIP User'
)
)
)
)
)
Any idea what the problem is? (This is an abstracted example)
I don’t see a problem with your queries, apart from the redundant
$andoperator. Consider the following Mongo shell example:Note that there isn’t a
role._idfield in my data, so it was simply ignored.If you’re having trouble with the JavaScript shell, it may be helpful to walk through the examples in the $or and $and documentation. Both operators expect an array as their value, which in turn can contain any number of valid criteria objects.
The main benefit of
$andis when you need to satisfy multiple criterion for the same field. If the field names are different, it’s equivalent to simply using a criteria object (where keys are implicitly and-ed).