How to resolve mongoDB C++ results returned by find method?

462 views
Skip to first unread message

zhaojun...@gmail.com

unread,
Aug 12, 2016, 7:14:10 AM8/12/16
to mongodb-user
In mongoDB database iPodia and collection OVS_DETAILS, I have a record as

{ 

   
"_id" : ObjectId("57ab14508b16c9557dcfa316"),
   
"dpid" : "202481588545212",
   
"mac" : "b8:27:eb:28:a6:bc",
   
"extranet_gateway_mac" : "f0:b4:29:52:8f:b6",
   
"extranet_gateway_ip" : "192.168.31.1",
   
"extranet_public_ip" : "59.66.214.24",
   
"extranet_private_ip" : "192.168.31.118",
   
"extranet_netmask" : "255.255.255.0",
   
"intranet_cidr_prefix" : 22020096,
   
"intranet_cidr_length" : 29,
   
"persist" : 0,
   
"timestamp" : 1470187766
}

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

How can I resolve the result? For example, get the value with key "persist". Many thanks.

Matt Cotter

unread,
Aug 12, 2016, 10:40:44 AM8/12/16
to mongodb-user
Hi,

If you're talking about accessing values in the document, please see this example:
https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/bsoncxx/getting_values.cpp

For your case, you probably want something like doc["persist"].

Best,
-Matt

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9e5c592a-d1cb-452f-9606-3d870ef1ad6a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

zhaojun...@gmail.com

unread,
Aug 12, 2016, 8:41:13 PM8/12/16
to mongodb-user
Thanks.

I am using 

bsoncxx::to_json(doc["timestamp"].get_value())

to get value with key "timestamp" in MongoDB.

But what I get is in scientific format string as "1.47019e+09". How can I get accurate value of key "timestamp"?

在 2016年8月12日星期五 UTC+8下午10:40:44,Matt Cotter写道:

David Golden

unread,
Aug 12, 2016, 8:59:33 PM8/12/16
to mongodb-user
You're trying to do too many things at once.

Assuming "timestamp" is stored in BSON as an integer type, you need to extract it as an integer.  I'll break it down for you (note, I haven't tested this code).

bsoncxx::document::element e = doc["timestamp"];  // element is a non-owning view of a single key/value pair

// Check that the element exists and is of the right type
if ( e && e.type() = bsoncxx::type::k_int64 ) {
   
// extract value as bsoncxx::types::b_int64, which converts to int64_t
    int64
_t timestamp = e.get_int64();
}


So the general pattern is "get the element, check its type, then extract value with the right getter for the type".

Regards,
David
Reply all
Reply to author
Forward
0 new messages