本文整理汇总了C++中BSONObj::elems方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONObj::elems方法的具体用法?C++ BSONObj::elems怎么用?C++ BSONObj::elems使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONObj
的用法示例。
在下文中一共展示了BSONObj::elems方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
// As of legacy-1.0-rc1-pre, you must initialize the driver in order to use
// the BSON library OID class.
mongo::client::GlobalInstance instance;
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
std::cout << "build bits: " << 8 * sizeof(char*) << '\n' << std::endl;
/* a bson object defaults on construction to { } */
BSONObj empty;
std::cout << "empty: " << empty << std::endl;
/* make a simple { _id : <generated>, when : <now>, name : 'joe', age : 33.7 } object */
{
const mongo::OID generated = mongo::OID::gen();
std::cout << "Generated an OID: " << generated << std::endl;
BSONObjBuilder b;
b.append("_id", generated);
b.append("when", mongo::jsTime());
b.append("name", "joe");
b.append("age", 33.7);
BSONObj result = b.obj();
std::cout << "json for object with _id: " << result << std::endl;
}
/* make { name : 'joe', age : 33.7 } with a more compact notation. */
BSONObj x = BSONObjBuilder().append("name", "joe").append("age", 33.7).obj();
/* convert from bson to json */
std::string json = x.toString();
std::cout << "json for x:" << json << std::endl;
/* access some fields of bson object x */
std::cout << "Some x things: " << x["name"] << ' ' << x["age"].Number() << ' ' << x.isEmpty()
<< std::endl;
/* make a bit more complex object with some nesting
{ x : 'asdf', y : true, subobj : { z : 3, q : 4 } }
*/
BSONObj y = BSON("x"
<< "asdf"
<< "y" << true << "subobj" << BSON("z" << 3 << "q" << 4));
/* print it */
std::cout << "y: " << y << std::endl;
/* reach in and get subobj.z */
std::cout << "subobj.z: " << y.getFieldDotted("subobj.z").Number() << std::endl;
/* alternate syntax: */
std::cout << "subobj.z: " << y["subobj"]["z"].Number() << std::endl;
/* fetch all *top level* elements from object y into a vector */
std::vector<BSONElement> v;
y.elems(v);
std::cout << v[0] << std::endl;
/* into an array */
std::list<BSONElement> L;
y.elems(L);
BSONObj sub = y["subobj"].Obj();
iter(y);
return 0;
}