How to parse data from mongocxx::cursor ncursor?

142 views
Skip to first unread message

Yashashree Jadhav

unread,
Jul 24, 2018, 7:09:04 AM7/24/18
to mongodb-dev
Hello,


I want to parse  data from ncursor. Is any one have experience with this.

 mongocxx::cursor ncursor = collectionpddl.find({});
 
    for(auto doc : ncursor)
    {  
    
          if( bsoncxx::to_json(doc) == "_id")
          {
                 std::cout << "parse" << "\n";
                
          }
              
    }

Kindly help me out.

Thank you and Regards.

Andrew Morrow

unread,
Jul 24, 2018, 1:17:40 PM7/24/18
to mongodb-dev

I don't think this line of code does what you intend it to do:

if( bsoncxx::to_json(doc) == "_id")

The JSON representation of the entire document yielded by the cursor almost certainly isn't equivalent to the string "_id", so the code inside the conditional will never be executed.

In order to interact with the sub-elements of the returned document, including the _id element, you should obtain a view over the document and then access items via subscript notation. To access the _id field, you would write something like this:

mongocxx::cursor ncursor = collectionpddl.find({});
for( auto doc : ncursor)  {
    std::cout << doc.view()["_id"].get_oid().value.to_string(); << "\n";
}

For more examples, I recommend looking at examples/mongocxx/get_values_from_documents.cpp

Thanks,
Andrew

--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev...@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-dev.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-dev/992d1bdf-bbee-4abc-83ec-beb564f8b014%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yashashree Jadhav

unread,
Jul 25, 2018, 2:15:03 AM7/25/18
to mongodb-dev
Thank you so much for logical explanation. Actually I need to generate value in normal format so that I can use this value for automatic generation of pddl file.

Your explanation gave me starting point. I will follow this direction.

Thank you and Regards.

Yashashree Jadhav

unread,
Jul 27, 2018, 12:31:48 PM7/27/18
to mongodb-dev
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

    mongocxx::cursor ncursor = collectionpddl.find({});
    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.

Andrew Morrow

unread,
Jul 31, 2018, 11:08:11 AM7/31/18
to mongodb-dev

It is hard to see what is going on here because the code is incomplete and the output you have provided doesn't appear to be generated by the code you have shown.

Could you please provide a complete example? I suspect the issue is simply that either the fields you are trying to extract don't exist in the sub-document that you are trying to extract them from, or that they aren't all strings (utf-8). You would need to examine the actual type of the element and deal with each case separately.

Thanks,
Andrew


--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev...@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-dev.

Yashashree Jadhav

unread,
Aug 1, 2018, 2:38:08 AM8/1/18
to mongodb-dev
Hello,

I have solved that issue. Thank you for your kind help.

You are right the issue was simple I need to handle each case separately while I was trying to handle it in group that's why I was getting that error "types is not in a document".

Thanks,
Yashashree
Reply all
Reply to author
Forward
0 new messages