[CDRIVER-468] mongoc_collection_find and logical $and/$or Created: 22/Nov/14  Updated: 03/May/17  Resolved: 27/Jan/15

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: 1.1.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: John Myers Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

*nix


Issue Links:
Duplicate

 Description   

When construction a query using BCON_NEW, using the $and or $or operator will fail if there is another expression prior to the use of $and/$or.

The following query will yield results:
query = BCON_NEW("$and","[","{",GROUPSTART,"

{","$lte",BCON_INT32(addr),"}","}","{",GROUPSTOP,"{","$gte",BCON_INT32(addr),"}","}","]");

The following will NOT yield results:
query = BCON_NEW(GROUPAUTO, BCON_BOOL(true),
"$and","[","{",GROUPSTART,"{","$lte",BCON_INT32(addr),"}

","}","{",GROUPSTOP,"

{","$gte",BCON_INT32(addr),"}

","}","]");

If I call a bson_as_json on the second query, and use that json to create a raw query from the shell. It WILL yield results.



 Comments   
Comment by Mira Carey [ 12/Jan/15 ]

Unfortunately the C driver has some overly aggressive validation for queries.

From the specification for the wire protocol (which find thinly wraps):

query
BSON document that represents the query. The query will contain one or more elements, all of which must match for a document to be included in the result set. Possible elements include $query, $orderby, $hint, $explain, and $snapshot.

Those are all viable top level $-prefixed keys that change the behavior of query. You can "escape" your query by creating a nested subdocument under a $query key.

Hope that helps,
Jason

Comment by John Myers [ 22/Nov/14 ]

Actually, it looks like combining an $and or $or expression with any other expression, will not work with the C driver.

Comment by John Myers [ 22/Nov/14 ]

This is the output from the failed C query that does work in the shell:
{ "auto_add" : true, "$and" : [ { "start_ip_int" :

{ "$lte" : 168428850 }

}, { "stop_ip_int" :

{ "$gte" : 168428850 }

} ] }

I am also aware that I could omit the $and since it is implicit if I just use the three expressions. But I was testing something else and inadvertently found this.

Generated at Wed Feb 07 21:09:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.