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

Force generate oid in mongo cxx client

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: legacy-1.1.0, legacy-1.1.1
    • Component/s: API, BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      hi, I am a develop from LeTV China.

      I use mongo cxx client ver. legacy, and have a question with the oid of the insert operation.

      In the github wiki, I have read below:

      Use the GENOID helper to add an object id to your object. The server will add an _id automatically if it is not included explicitly.

      BSONObj p = BSON( GENOID << "name" << "Joe" << "age" << 33 );
      // result is:

      Unknown macro: { _id }

      BSONObj p = BSONObjBuilder().genOID().append("name","Joe").append("age",33).obj();

      It means that mongo client will not generate the oid for me unless I included it explicitly in the BSONbuilder.
      But in `insert_write_operation.cpp`, I found that mongo client will gerate oid for me automatically.

          BSONObj InsertWriteOperation::_ensureId(const BSONObj& doc) {
              BSONElement id = doc.getField("_id");
              if (!id.eoo()) {
                  uassert(0,
                          "value of _id element cannot contain any fields starting with $",
                          !id.isABSONObj() || id.Obj().okForStorage());
                  return doc;
              }   
      
              BSONObjBuilder bob;
              bob.append("_id", OID::gen());
              bob.appendElements(doc);
              return bob.obj();
          }
      

      So, will the mongo client give the choice to user to select who will genrate the oid, from the mongo client or the mongo server.

            Assignee:
            andrew.morrow@mongodb.com Andrew Morrow (Inactive)
            Reporter:
            unpeeled_onion@outlook.com ethan zhang
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: