The function "mongodb_field_storage_query" doesn't work properly with multiple conditions.

PHP Version 5.3.10-1ubuntu3.11
MongoDB Driver 1.4.5

It *isn't* possible to execute the following query:

mongodb command

db['fields_current.message'].find({
    $and: [
        {'field_msg_status.value': {$in: ['S', 'Q']}},
        {'field_msg_sender.target_id': 4445}
    ]
})

Given the current array structure generated by the module, it seems to send a query like this:

db['fields_current.message'].find({
        'field_msg_status.value': {$in: ['S', 'Q']},
        'field_msg_sender.target_id': 4445
})

Notice the "$and" element in the first level. ie, The function must implement explicit AND conditions.

Reference:

MongoDB provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.

-- Reference: http://docs.mongodb.org/manual/reference/operator/query/and

Comments

math3usmartins’s picture

Just a suggestion on how to fix that issue.

math3usmartins’s picture

Issue summary: View changes
math3usmartins’s picture

Issue summary: View changes
math3usmartins’s picture

Issue summary: View changes
Anonymous’s picture

Status: Active » Needs review
majorrobot’s picture

Edit: Created a separate issue from the below comment at #2551689: Field Query does not allow multiple conditions on the same field using same operator, since it can technically be solved separately.

While reviewing the above patch, I realized that there's actually a bigger issue here. While it's true that the module isn't using an explicit $and where necessary, it's also not allowing multiple conditions on a single field. So, there's not even a way to create a case where the explicit $and would even be necessary.

Specifically, lines 370-380 will overwrite conditions on a field if there are more than one:
...
So, if I have an EFQ like:
...
Only the second fieldCondition utilizing field_color will be used. This kills the benefits of chaining methods in an EFQ and is very likely causing inaccurate results to be returned for many users.

Working on a patch now that will put conditions for each field in an array instead.

majorrobot’s picture

The patch in #1 works for me, with one small change: switching is_array($find) to !empty($find), since $find is, by default, always an array. Attaching updated patch.