array in mongodb ( c++ driver)ITeye - 娱乐之横扫全球

array in mongodb ( c++ driver)ITeye

2019-01-12 12:53:20 | 作者: 语海 | 标签: | 浏览: 2538

BSONArray
BSONArrayBuilder 

BSON Arrays in C++



 db.mongodb.insert({"dbName": "343", hostPort: ["127.0.0.1:1001", "127.0.1.2:4000"]})
 db.mongodb.find()
{ "_id" : ObjectId("4f97b77afdc44ff960010ce2"), "dbName" : "343", "hostPort" : [ "192.168.0.191:30000" ] }
{ "_id" : ObjectId("4f97c3f3fdc44ff960010ce5"), "dbName" : "343", "hostPort" : [ "127.0.0.1:1001", "127.0.1.2:400
0" ] }


string mongodbCollection = "test.mongodb";
auto_ptr mongo::DBClientCursor cursor = c- query( mongodbCollection , mongo::BSONObj() );
int count = 0;
while ( cursor- more() ) {
 count++;
 mongo::BSONObj obj = cursor- next();
 const char *pDbName = obj.getStringField("dbName");
 mongo::BSONElement hostPortElement = obj.getField("hostPort");
 if (!hostPortElement.eoo() 
 hostPortElement.type() == mongo::Array
 vector mongo::BSONElement hostPorts = hostPortElement.Array();
 for (vector mongo::BSONElement ::iterator it = hostPorts.begin(); it != hostPorts.end(); ++it)
 string temp;
 it- Val(temp);
 cout temp.c_str() endl;


// examples
using namespace mongo;
using namespace bson;
bo an_obj;
/** transform a BSON array into a vector of BSONElements.
 we match array # positions with their vector position, and ignore
 any fields with non-numeric field names.
vector be a = an_obj["x"].Array();
be array = an_obj["x"];
assert( array.isABSONObj() );
assert( array.type() == Array );
// Use BSON_ARRAY macro like BSON macro, but without keys
BSONArray arr = BSON_ARRAY( "hello" 1 BSON( "foo" BSON_ARRAY( "bar" "baz" "qux" ) ) );
// BSONArrayBuilder can be used to build arrays without the macro
BSONArrayBuilder b;
b.append(1).append(2).arr();
/** add all elements of the object to the specified vector */
bo myarray = an_obj["x"].Obj();
vector be 
myarray.elems(v);
list be 
myarray.elems(L)                          
			
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表娱乐之横扫全球立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章