[CDRIVER-2543] mongoc_collection_find_with_opts() does not work with multiple filters Created: 10/Mar/18 Updated: 27/Oct/23 Resolved: 12/Mar/18 |
|
| Status: | Closed |
| Project: | C Driver |
| Component/s: | None |
| Affects Version/s: | 1.5.0, 1.6.0, 1.7.0 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Liviu Chircu | Assignee: | A. Jesse Jiryu Davis |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | filter | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
MongoDB 2.6 / MongoDB 3.4 |
||
| Description |
|
As soon as I moved to 1.7.0 and rewrote the code to use `mongoc_collection_find_with_opts()`, my filtering tests are starting to fail. Assuming we're working with this collection:
My pre-libmongoc-1.7.0 testing code used to construct the following filters (printed with `bson_as_json()` here) then pass them to `mongoc_collection_find()`:
Now, I have updated the code to match the 1.7.0 API. Thanks to the new function, we can simplify the filters – here are the JSON views of the new filters, along with their results after passing them to the new `mongoc_collection_find_with_opts()`:
Moreover, the 2nd query literally works if I hit it in the console, so either I'm doing something terribly wrong (forgetting some flag / API call / shouldn't be doing string comparisons anymore, etc.), or there is a good chance there is a bug in the 1.7.0 driver. Any advice would be appreciated! Thanks! |
| Comments |
| Comment by A. Jesse Jiryu Davis [ 12/Mar/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
No, there's been no change in the BSON-building interface, the functions you're using aren't deprecated. They would be marked deprecated in the documentation if they were. Appending to an array requires numeric keys: "0", "1", "2", and so on. You can't append to an array with an empty key, that's what the "Invalid filter: empty key" error indicates. Try using numeric keys, and also make sure that you correctly specify the length of "$gte" as 4, not 3:
I get the same behavior with 1.5.0 as I do with the latest code: you just need to create the filter array correctly for the query to succeed. For convenience you can try the BSON_APPEND_UTF8 macro which doesn't require a key or value length, so you don't have to count characters yourself. And see the code example in this tutorial for using bson_uint32_to_string to create array keys. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Liviu Chircu [ 12/Mar/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thank you for the nice example, Jesse! I can, indeed, confirm that the BCON API works as you posted. However, I was using the former API, which still does not work. Sample program below:
This produces:
So, morale of the story: are the BSON functions I'm using completely deprecated? Again: this code works previously to 1.5.0! Thanks! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by A. Jesse Jiryu Davis [ 11/Mar/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks for the response. I see that my suggestion about "filter" was misguided, forget that. However, I can't reproduce the bug you report. I've created a collection called "test.test" with the following data:
The following code uses a query like { "$and" : [ { "foo" : { "$lt" : "D" } }, { "foo" : { "$gte" : "A" } } ] } and it retrieves 3 results, using libmongoc 1.7.0:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Liviu Chircu [ 10/Mar/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Prepending either "$filter" or "filter" seems to make things worse in 1.7.0. The following basic test does not work anymore:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by A. Jesse Jiryu Davis [ 10/Mar/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks, I have a hunch you're correct, there's a bug and I think I know what it is. Does mongoc_collection_find_with_opts give the correct results if you use a field named "filter" in the place you used to use "query"?:
|