Discussion:
[mongodb-dev] How to parse data from mongocxx::cursor ncursor?
Yashashree Jadhav
2018-07-24 10:15:47 UTC
Permalink
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.
--
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 mongodb-***@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.
'Andrew Morrow' via mongodb-dev
2018-07-24 17:17:26 UTC
Permalink
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
<https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/get_values_from_documents.cpp#L91>

Thanks,
Andrew

On Tue, Jul 24, 2018 at 7:09 AM Yashashree Jadhav <
Post by Yashashree Jadhav
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.
--
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
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
<https://groups.google.com/d/msgid/mongodb-dev/992d1bdf-bbee-4abc-83ec-beb564f8b014%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 mongodb-***@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/CAHX05qGVzicFo28zqJm2Zv2ASdw5wefzztQ%2BM%2Bjh4wf8-zoL2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Yashashree Jadhav
2018-07-25 06:15:02 UTC
Permalink
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.
Post by Yashashree Jadhav
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
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
<https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/get_values_from_documents.cpp#L91>
Thanks,
Andrew
Post by Yashashree Jadhav
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.
--
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
<javascript:>.
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
<https://groups.google.com/d/msgid/mongodb-dev/992d1bdf-bbee-4abc-83ec-beb564f8b014%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 mongodb-***@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/a9ba12fc-6129-4580-9ca7-bc50d8e9c7fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Yashashree Jadhav
2018-07-27 16:31:48 UTC
Permalink
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.
--
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 mongodb-***@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/9b72d831-d225-49b3-8f9e-5c5133c17135%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Andrew Morrow' via mongodb-dev
2018-07-31 15:07:56 UTC
Permalink
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


On Fri, Jul 27, 2018 at 12:31 PM Yashashree Jadhav <
Post by Yashashree Jadhav
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.
--
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
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/9b72d831-d225-49b3-8f9e-5c5133c17135%40googlegroups.com
<https://groups.google.com/d/msgid/mongodb-dev/9b72d831-d225-49b3-8f9e-5c5133c17135%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 mongodb-***@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/CAHX05qEHSNtHhCMj%3DaonY2q5n7xa-NN8i2U_dA7xg0RXm28AMg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Yashashree Jadhav
2018-08-01 06:38:08 UTC
Permalink
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
Post by 'Andrew Morrow' via 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
Post by Yashashree Jadhav
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.
--
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
<javascript:>.
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/9b72d831-d225-49b3-8f9e-5c5133c17135%40googlegroups.com
<https://groups.google.com/d/msgid/mongodb-dev/9b72d831-d225-49b3-8f9e-5c5133c17135%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 mongodb-***@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/e145893e-e54a-414a-b369-72af80a21528%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...