Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-1001

mongocxx::cursor.begin() increments the iterator

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.1.0
    • Affects Version/s: 3.0.1
    • Component/s: API
    • Labels:
      None
    • Environment:
      Windows 10
      MS VS 2015
      MongoDB 3.3.5
      MongoDB C++11 Driver 3.0.1
      Using WiredTiger

      Simple program to create and print out two records:

      main.cpp
      #include <bsoncxx/builder/stream/document.hpp>
      #include <bsoncxx/types.hpp>
      #include <bsoncxx/json.hpp>
      #include <mongocxx/client.hpp>
      #include <mongocxx/instance.hpp>
      #include <mongocxx/uri.hpp>
      #include <iostream>
      
      int main()
      {
      	mongocxx::instance inst{};
      	mongocxx::client conn{ mongocxx::uri{} };
      
      	auto db = conn["testcursor"];
      
      	db["names"].drop();
      
      	bsoncxx::document::value document = bsoncxx::builder::stream::document{} << "name" << "George" << bsoncxx::builder::stream::finalize;	
      	db["names"].insert_one(std::move(document));
      	document = bsoncxx::builder::stream::document{} << "name" << "Mark" << bsoncxx::builder::stream::finalize;
      	db["names"].insert_one(std::move(document));
      
      	auto cursor = db["names"].find({});
      
      	for (auto&& doc : cursor) {
      		std::cout << bsoncxx::to_json(doc) << std::endl;
      	}
      	return 0;
      }
      

      Output (as expected):

      {
          "_id" : {
              "$oid" : "57b8b1209853f297f00005c1"
          },
          "name" : "George"
      }
      {
          "_id" : {
              "$oid" : "57b8b1209853f297f00005c2"
          },
          "name" : "Mark"
      }
      

      Now, if I insert into the code cursor.begin(); right after the find():

      	auto cursor = db["names"].find({});
      	
              cursor.begin();
          
      	for (auto&& doc : cursor) {
      		std::cout << bsoncxx::to_json(doc) << std::endl;
      	}
      

      Then my output is (unexpectedly missing the "George" entry):

      {
          "_id" : {
              "$oid" : "57b8b1c99853f26e68007472"
          },
          "name" : "Mark"
      }
      

      My objective was to test if the cursor was empty (there is no .empty()) so I tried to use:

      if ( cursor.begin() == cursor.end() ) but that seemingly advances the iterator?

            Assignee:
            david.golden@mongodb.com David Golden
            Reporter:
            therefore George Thompson
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: