Search code examples
cmongodbmongo-c-driver

Get a cursor for an entire collection with mongo c driver


I am using mongo c driver 1.1 with mongo version 3.0. I need to write a function that gets a cursor to a collection. I found the following example in the documentation.

http://api.mongodb.org/c/1.1.0/mongoc_cursor_t.html

   collection = mongoc_client_get_collection (client, "test", collection_name);
   cursor = mongoc_collection_find (collection,
                                    MONGOC_QUERY_NONE,
                                    0,
                                    0,
                                    0,
                                    &query,
                                    NULL,  /* Fields, NULL for all. */
                                    NULL); /* Read Prefs, NULL for default */

I want to do exactly this, except i want to have no query (Match all documents in the collection)


Solution

  • You can use an empty query specifier to find all documents in the database "mydb" and collection "mycoll" as in the following snippet:

    collection = mongoc_client_get_collection (client, "mydb", "mycoll");
    query = bson_new ();
    cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
    

    Full documentation here.