array in mongodb ( c++ driver)

BSONArray
BSONArrayBuilder

BSON Arrays in C++
[url]http://www.mongodb.org/display/DOCS/Updating#Updating-PushingaUniqueValue[/url]



> 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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值