For example, if I do this in shell
> db.numbers.save( { name: "fibonacci", arr: [0, 1, 1, 2, 3, 5, 8, 13, 21] } )
Then i want to get arr
in my c++ program.
After i got BSONObj i can get name
with
std::string name = p.getStringField("name");
where p
is a BSON object.
But what is the right way to get elements from array and save them into std::vector?
EDIT:
After some more research i found BSONElement doxygen documentation and made this.
std::vector<int> arr;
std::vector<BSONElement> v = p.getField("arr").Array();
for(std::vector<BSONElement>::iterator it = v.begin(); it != v.end(); ++it)
arr.push_back(it->numberInt());
But im still not sure if it is the right way.