[CDRIVER-411] find() with a limit ignores documents not fitting in the initial batch Created: 20/Aug/14  Updated: 19/Oct/16  Resolved: 21/Aug/14

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

Type: Bug Priority: Blocker - P1
Reporter: Hannes Magnusson Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CDRIVER-982 "limit" not used in OP_QUERY message ... Closed

 Description   

calling mongoc_collection_find() with batch_size of 0 and a limit of 10000 will set the wireprotocol numberToReturn to -10000.
When setting a batch_size, it also seems to be passed in as a negative number to numberToReturn for unknown reason.

This does not work. If the collection has plenty big collections MongoDB will only send it data fitting into the initial reply and then ignore everything after that.
This is why all drivers set numberToReturn to 0, or positive number, not a negative number by default.



 Comments   
Comment by Mira Carey [ 21/Aug/14 ]

removed the logic to send negative nToReturn. That should fix the issue

Comment by Mira Carey [ 21/Aug/14 ]

https://github.com/mongodb/mongo-c-driver/commit/083498bb4f2c5782fa2c0528472306bad08d3f57

Comment by Hannes Magnusson [ 20/Aug/14 ]

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>
 
int
main (int   argc,
      char *argv[])
{
        mongoc_client_t *client;
        mongoc_collection_t *collection;
        mongoc_cursor_t *cursor;
        const bson_t *doc;
        bson_t *query;
        char *str;
        int64_t count = 0;
        int64_t got = 0;
 
        mongoc_init ();
 
        client = mongoc_client_new ("mongodb://localhost:27017/");
        collection = mongoc_client_get_collection (client, "enron_mail", "messages");
        query = bson_new ();
        count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, query, 0, 0, NULL, NULL);
        cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 100000, 0, query, NULL, NULL);
 
        while (mongoc_cursor_next (cursor, &doc)) {
                got++;
        }
        printf("Got in total: %ld documents, out of %ld\n", got, count);
 
        bson_destroy (query);
        mongoc_cursor_destroy (cursor);
        mongoc_collection_destroy (collection);
        mongoc_client_destroy (client);
 
        return 0;
}
}

Got in total: 2035 documents, out of 501513

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