[CXX-1995] Standard string from bson to son to look up the specific value Created: 15/Apr/20  Updated: 27/Oct/23  Resolved: 24/Aug/20

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: 莫 莫 Assignee: Samantha Ritter (Inactive)
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hello dear MongoDB guys,

I can visit a mongodb data-base in remote server, get the data-base and correponding collection.

I filter kip with the specific ids, the information can be displayed as the standard string, however, it cannot be queried with the interested keys to get the data-list.

My code is:

int main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
// must remain alive for as long as the driver is in use.
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
mongocxx::instance inst{};

mongocxx::client conn{mongocxx::uri{"mongodb://192.168.7.202/#video-label"}};
/* list databases */
for(auto doc : conn.list_databases())

Unknown macro: { std}

/* access or create a database */
mongocxx::database db = conn["workstation"];
mongocxx::collection collection = db["videolabels"];
/* query one doc that match with filter */
mongocxx::cursor cursor = collection.find(make_document(kvp("id", "m-2417")));
std::string raw_json = "";
for (auto doc : cursor)

Unknown macro: { raw_json = bsoncxx}

const auto raw_json_length = static_cast<int>(raw_json.length());
JSONCPP_STRING err;
Json::Value root;
Json::CharReaderBuilder builder;
Json::Reader old_reader;

if(old_reader.parse(raw_json,root))

Unknown macro: { std}

const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
if (!reader->parse(raw_json.c_str(), raw_json.c_str() + raw_json_length, &root,
&err))

Unknown macro: { std}

std::cout<<root.asString() << std::endl;
const std::string keyboard_str = root["keyboard"].asString();
std::cout<<keyboard_str <<std::endl;
}

As I know, the jsoncpp and other corresponding C++ library can only parse the string with the backslash between double-quote. With standard string as input, I cannot get the "root["keyboard"].asString()". Is there any solution to resolve this? The returned value of bsoncxx and mongocxx API can be queried by jsoncpp.
Thanks & Regards!
Momo



 Comments   
Comment by Samantha Ritter (Inactive) [ 22/Jul/20 ]

Hi 1921shanghai@gmail.com,

Thank you for opening this ticket!

Are you able to provide the structure of the documents you are using? I'm guessing that they have a field called "keyboard" with a string value, like this:

{ "keyboard" : "Yamaha" }

I think that you are asking about how to access the value of the "keyboard" field. Here's how to do that:

// Construct the document
auto doc = make_document(kvp("keyboard", "Yamaha"));
 
// "doc" is a document::value, but the document::view class has all the convenience functions
auto doc_view = doc.view();
 
// Access the "keyboard" field
auto keyboard_value = doc_view["keyboard"].get_utf8().value;

Does that help?

Thanks,
Samantha Ritter

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