Hello,
Your solution work file for parsing ID from given data. But when I tried to extract other information it only give me one value and didn't work for remaining values. I might be wrong in implementation.
I am trying to figure out. Your guidance will be helpful.
Thank you and Regards.
Code that works fine and give accurate ID
for( const bsoncxx::document::view& doc : ncursor)
{
std::cout << bsoncxx::to_json(doc) <<std::endl;
using bsoncxx::type;
// Extract _id element as a string.
print_type(doc,pFile);
print_predicate(doc,pFile);
print_function(doc,pFile);
bsoncxx::document::element id_ele = doc["_id"];
if (id_ele.type() == type::k_oid)
{
std::string oid = id_ele.get_oid().value.to_string();
std::cout << "OID: " << oid << std::endl;
pFile << oid << std::endl;
}
else
{
std::cout << "Error: _id was not an object ID." << std::endl;
}
//pFile << bsoncxx::to_json(doc) << std::endl;
}
Below the code having some problem
void print_type(const bsoncxx::document::view& doc, std::ofstream& pFile)
{
using bsoncxx::type;
bsoncxx::document::element ele;
bsoncxx::document::element msg = doc["types"];
if (msg && msg.type() == type::k_utf8)
{
std::cout << "types: " << msg.get_utf8().value << std::endl;
pFile << msg.get_utf8().value << std::endl;
}
else
{
std::cout << "types is not in a document" << std::endl;
}
}
Output
{
"_id" : {
"$oid" : "5b5afc94b46d4a14153ff2e1"
},
"hello" : "mono"
}
types is not in a document
predicates is not in a document
predkey is not in a document
predvalue is not in a document
function is not in a document
funckey is not in a document
funcvalue is not in a document
OID: 5b5afc94b46d4a14153ff2e1
{
"_id" : {
"$oid" : "5b5afc94b46d4a14153ff2e2"
},
"types" : "waypont",
"types" : "robot"
}
types: waypont
predicates is not in a document
predkey is not in a document
predvalue is not in a document
function is not in a document
funckey is not in a document
funcvalue is not in a document
OID: 5b5afc94b46d4a14153ff2e2
Above in output we can see it accurately extract all id but only one time extract type value.