[CXX-887] Create an example of getting _id's from insert results Created: 01/Apr/16 Updated: 11/Sep/19 Resolved: 14/Jun/16 |
|
| Status: | Closed |
| Project: | C++ Driver |
| Component/s: | API |
| Affects Version/s: | None |
| Fix Version/s: | 3.0.2 |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | Armin Ball [X] | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | c++11, neweng | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
| Description |
|
Hey there, I'm using c++11 driver and going to extract the Hex-ID of a youst inserted document with the function collection.insert_one. Is there a function I can coll to extract the ID out of the returned Object? struct core::v1::optional<mongocxx::v_noabi::result::insert_one> retVal3 = collection.insert_one(document.view()); std::string = retVal3.IDplease(); // or the hex code. anything? =) |
| Comments |
| Comment by Githook User [ 14/Jun/16 ] | ||||||||||||||||||||
|
Author: {u'username': u'xdg', u'name': u'David Golden', u'email': u'xdg@xdg.me'}Message: | ||||||||||||||||||||
| Comment by Armin Ball [X] [ 08/Jun/16 ] | ||||||||||||||||||||
|
cool =) | ||||||||||||||||||||
| Comment by David Golden [ 06/Jun/16 ] | ||||||||||||||||||||
| Comment by Andrew Morrow (Inactive) [ 11/Apr/16 ] | ||||||||||||||||||||
|
Weltenbummler - Yes we would of course be interested in taking a pull request with a new example if you are interested in submitting one. You will need to sign the MongoDb contributor agreement. | ||||||||||||||||||||
| Comment by Armin Ball [X] [ 08/Apr/16 ] | ||||||||||||||||||||
|
I thank you for your support! | ||||||||||||||||||||
| Comment by Matt Cotter [ 07/Apr/16 ] | ||||||||||||||||||||
|
Hi Armin, Try something like this:
It prints:
The $oid is just a way that we represent object id's in pure JSON (because using the "_id" : ObjectId("5705918cb6a7d12aea0525f1") is not true json, but extended json). I am going to retitle this ticket to add an example for the code snippet I posted to the examples folder in the repository. For any future bug reports, please open new tickets. Thanks again for using the driver! -Matt | ||||||||||||||||||||
| Comment by Armin Ball [X] [ 07/Apr/16 ] | ||||||||||||||||||||
|
Hey there, I'm going to get the document withe the ID 57040cd5b6a7d149e30537b3 from my database. I tried:
nothing worked. To get to this attempt I spend some hours. Maybe you could solve my problem some more faster than me the structure is like:
Btw. what is the meaning of the "$oid" ? | ||||||||||||||||||||
| Comment by Armin Ball [X] [ 06/Apr/16 ] | ||||||||||||||||||||
|
Dear Matt, thank you for your lines. I already have some questions left: result optional is engaged ("true")
Is this the only way to catch the error? How should I test the Type?
How can I find that function, you just introduced to me?
what tells me, that this is a function that I can use on the returning type of: auto db = conn["test"]["log"]; where can I look for in the library source files? All the best! | ||||||||||||||||||||
| Comment by Armin Ball [X] [ 05/Apr/16 ] | ||||||||||||||||||||
|
You can't believe how glad I'm right now!
| ||||||||||||||||||||
| Comment by Matt Cotter [ 05/Apr/16 ] | ||||||||||||||||||||
|
Hi Weltenbummler, Great question! I think the inserted_id() method of the result::insert_one class is what you are looking for. Here's an example:
Note, to be safe here, you want to double check that the result optional is engaged ("true") before accessing into it, and also that the bsoncxx::types::value returned from inserted_id() has the right type before calling get_oid(). Let me know if this answers your question! Best, | ||||||||||||||||||||
| Comment by Armin Ball [X] [ 02/Apr/16 ] | ||||||||||||||||||||
|
Maybe this is the wrong place. please inform me, if so. |