BSONArrayBuilder
BSON Arrays in C++
http://www.mongodb.org/display/DOCS/Updating#Updating-PushingaUniqueValue
> 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> v; myarray.elems(v); list<be> L; myarray.elems(L) /** add all values of the object to the specified collection. If type mismatches, exception. template <class T> void Vals(vector<T> &) const; template <class T> void Vals(list<T> &) const; */ /** add all values of the object to the specified collection. If type mismatches, skip. template <class T> void vals(vector<T> &) const; template <class T> void vals(list<T> &) const; */
http://docs.mongodb.org/ecosystem/drivers/cpp-bson-array-examples/
http://docs.mongodb.org/manual/reference/operator/or/