[CXX-909] Force generate oid in mongo cxx client Created: 13/May/16  Updated: 17/May/16  Resolved: 16/May/16

Status: Closed
Project: C++ Driver
Component/s: API, BSON
Affects Version/s: legacy-1.1.0, legacy-1.1.1
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: ethan zhang Assignee: Andrew Morrow (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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.



 Comments   
Comment by ethan zhang [ 17/May/16 ]

ok, Thank you guys.

Comment by Andrew Morrow (Inactive) [ 16/May/16 ]

unpeeled_onion@outlook.com I'm closing this issue as the driver is working as designed. Whether or not the server or the driver does the work of inserting the _id, the observable effect is "as if" the server had done the _id generation. Clients should not care which way it is done, and in fact it is beneficial if it is done in the driver, as pointed out above, since otherwise there is no way for the driver to report to the user the _id of the object that it was inserted.

Comment by David Golden [ 16/May/16 ]

unpeeled_onion@outlook.com, drivers generate OIDs client-side so they can provide users with the _id of the document just inserted (as the server doesn't return _id on insert the way it does for upsert).

Comment by Andrew Morrow (Inactive) [ 16/May/16 ]

unpeeled_onion@outlook.com - Yes, but why does it matter? Why do you care if the server sets the _id field, or if the client does it? From the perspective of a user of the driver, the externally observable effects are the same. Almost all MongoDB drivers do client side _id generation. If, somehow, you are using a driver that doesn't, the server will then take care of it as a last resort.

Comment by ethan zhang [ 16/May/16 ]

Hi, Andrew
The GENOID macro is be working correctly for me. The above qutoe was wrriten in the github wiki https://github.com/mongodb/mongo-cxx-driver/wiki/Tutorial.
I means that the in the wiki, it said that _id insert by the server when I didn't add it in the bson query object. However, I check the code, whether I add the _id in bson query object or not, the _id will insert by the client. So, there is no choice for user to add _id by the server with the mongo cxx client.

Comment by Andrew Morrow (Inactive) [ 14/May/16 ]

First, are you saying that the GENOID macro isn't working correctly for you? I don't entirely understand the part about "Unknown macro".

As far as whether the _id field is inserted automatically by the server or the client, why does it matter? The observed effects are the same. As a user of the driver, if you insert an object without an _id, one will be added for you. Why should the application care whether that was done on the client or on the server?

Generated at Wed Feb 07 22:00:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.