[CDRIVER-1000] mongoc_collection_find fails with top level $or, etc. Created: 16/Nov/15  Updated: 03/May/17  Resolved: 17/Nov/15

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

Type: Bug Priority: Major - P3
Reporter: Matt Cotter Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

When trying to do

#include <mongoc.h>
#include <stdio.h>
#include <stdlib.h>
 
int
main (int   argc,
      char *argv[])
{
   bson_error_t error;
   const bson_t *out;
   bson_t *query;
   mongoc_client_t *client;
   mongoc_collection_t *collection;
   mongoc_cursor_t *cursor;
 
   mongoc_init ();
 
   client = mongoc_client_new ("mongodb://127.0.0.1:27017/");
   collection = mongoc_client_get_collection (client, "test", "restaurants");
   query = BCON_NEW("$or", "[", "{", "cuisine", "Italian", "}",
                                "{", "address.zipcode", "10036", "}", "]");
   cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, query, NULL, NULL);
 
   if (mongoc_cursor_next(cursor, &out)) {
      printf("document found\n");
   } else if (mongoc_cursor_error(cursor, &error)) {
      printf ("error: %s\n", error.message);
   } else {
      printf("no documents found\n");
   }
 
   bson_destroy (query);
   mongoc_cursor_destroy (cursor);
   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
 
   mongoc_cleanup ();
 
   return EXIT_SUCCESS;
}

the new mongoc_collection_find sends the command

{ find: "restaurants", or: [ { cuisine: "Italian" }, { address.zipcode: "10036" } ] }

which can’t be parsed by the server and results in an error.

error: Failed to parse: { find: "restaurants", or: [ { cuisine: "Italian" }, { address.zipcode: "10036" } ], limit: 1 }. Unrecognized field 'or'.



 Comments   
Comment by Githook User [ 17/Nov/15 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-1000 upgrade $or, etc. from OP_QUERY to find cmd
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/16815f8f719c4e24156e026e41ce948248a5c144

Comment by A. Jesse Jiryu Davis [ 16/Nov/15 ]

I see; I'll have to be more selective in how we translate queries.

Old plan:

$query -> filter
$orderby -> sort
$showDiskLoc -> showRecordId
$anything-else -> anything-else

New plan:

$query -> filter
$orderby -> sort
$hint -> hint
$comment -> comment
$maxScan -> maxScan
$maxTimeMS -> maxTimeMS
$max -> max
$min -> min
$returnKey -> returnKey
$showDiskLoc -> showRecordId
$snapshot -> snapshot
$anything-else -> ensure "filter" and push $anything-else into "filter" document

So this:

{$or: [a, b]}

is no longer translated into:

{find: "collection", or: [a, b], filter: {}}

Instead, it'll become:

{find: "collection", filter: {$or: [a, b]}}

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