{
"_id" : -767958333,
"messagelist" : [ {
"uid" : 413098706,
"status" : 2,
"uniqid" : "413098706:1441105719"
} ]
}
BSONObj bson_obj; (refer to the db above)
if (bson_obj.hasField("messagelist" && ((arr_value = bson_obj["messagelist"]).isNull())){
vector<BSONElement> msg_vec = arr_value.Array();
for (vector<mongo::BSONElement>::iterator it = message_vec.begin(); it != message_vec.end(); ++it) {
BSONObj msg_obj = it->embeddedObject();
// black magic happen here....
if (...) {
...
}
}
}
// Library
...
...
using bsoncxx::builder::stream::open_document;using bsoncxx::builder::stream::close_document;using bsoncxx::builder::stream::open_array;using bsoncxx::builder::stream::close_array;using bsoncxx::builder::stream::finalize;using namespace bsoncxx;using namespace std;
int magic(bsoncxx::builder::stream::document* document) {
document::value value{document->extract()}; document::view view{value.view()};
for (document::element ele : view) {
if(ele.type() == type::k_array) {
//@TODO
}
}
}
int main() {
mongocxx::instance inst{}; mongocxx::client conn{mongocxx::uri{"xxxx"}}; bsoncxx::builder::stream::document document{};
document << "_id" << "102030" << "messagelist" << open_array << open_document << "uid" << 23234234 << "status" << 2 << "uniqid" << "1212" << close_document << close_array;
int ret = magic(document);}
#include <cstdint>
#include <iostream>
#include <bsoncxx/array/view.hpp>
#include <bsoncxx/array/view.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/document/value.hpp>
#include <bsoncxx/document/view.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>
#include <bsoncxx/types/value.hpp>
using namespace bsoncxx;
document::value new_message(int64_t uid, int32_t status, std::string uniqueid) {
builder::stream::document doc;
return doc << "uid" << uid << "status" << status << "uniqueid" << uniqueid
<< builder::stream::finalize;
}
void iterate_messages(document::view doc) {
// extract "messagelist" element
document::element ele = doc["messagelist"];
// check validity && type before trying to iterate
if (ele && ele.type() == type::k_array) {
array::view subarray{ele.get_array().value};
for (array::element msg : subarray) {
std::cout << "array element: " << to_json(msg.get_value()) << std::endl;
}
} else {
std::cout << "Missing or invalid messagelist." << std::endl;
}
}
int main() {
builder::stream::document builder{};
builder << "messagelist" << builder::stream::open_array
<< new_message(413098706, 2, "413098706:1441105719")
<< new_message(413098707, 2, "413098707:1441105720")
<< new_message(413098708, 2, "413098708:1441105721")
<< builder::stream::close_array;
document::value doc = builder << builder::stream::finalize;
iterate_messages(doc.view());
}
void iterate_messages(document::view doc) {
// extract "messagelist" element
document::element ele = doc["messagelist"];
// check validity && type before trying to iterate
if (ele && ele.type() == type::k_array) {
array::view subarray{ele.get_array().value};
for (array::element msg : subarray) {
if (msg.type() == type::k_document) {
document::view subdoc{msg.get_document()};
if (subdoc["uid"] && subdoc["uid"].type() == type::k_int64) {
std::cout << "uid: " << subdoc["uid"].get_int64().value << std::endl;
}
if (subdoc["status"] && subdoc["status"].type() == type::k_int32) {
std::cout << "status: " << subdoc["status"].get_int32().value << std::endl;
}
if (subdoc["uniqueid"] && subdoc["uniqueid"].type() == type::k_utf8) {
std::cout << "uniqueid: " << subdoc["uniqueid"].get_utf8().value << std::endl;
}
} else {
std::cout << "Message is not a document" << std::endl;
}
}
} else {
std::cout << "Missing or invalid messagelist." << std::endl;
}
}