array in mongodb (c++)

BSONArray
BSONArrayBuilder 

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


Mongo shell代码
  1. > db.mongodb.insert({"dbName""343", hostPort: ["127.0.0.1:1001""127.0.1.2:4000"]})   
  2. > db.mongodb.find()   
  3. "_id" : ObjectId("4f97b77afdc44ff960010ce2"), "dbName" : "343""hostPort" : [ "192.168.0.191:30000" ] }   
  4. "_id" : ObjectId("4f97c3f3fdc44ff960010ce5"), "dbName" : "343""hostPort" : [ "127.0.0.1:1001", "127.0.1.2:400  
  5. 0" ] }   
  6. >  


C++代码
  1. string mongodbCollection = "test.mongodb";   
  2. auto_ptr<mongo::DBClientCursor> cursor = c->query( mongodbCollection , mongo::BSONObj() );   
  3. int count = 0;   
  4. while ( cursor->more() ) {   
  5.     count++;   
  6.     mongo::BSONObj obj = cursor->next();   
  7.     const char *pDbName = obj.getStringField("dbName");   
  8.     mongo::BSONElement hostPortElement = obj.getField("hostPort");   
  9.     if (!hostPortElement.eoo()    
  10.             &&hostPortElement.type() == mongo::Array   
  11.         )   
  12.     {   
  13.         vector<mongo::BSONElement> hostPorts = hostPortElement.Array();   
  14.         for (vector<mongo::BSONElement>::iterator it = hostPorts.begin(); it != hostPorts.end(); ++it)   
  15.         {   
  16.             string temp;   
  17.             it->Val(temp);   
  18.             cout << temp.c_str() << endl;   
  19.         }   
  20.     }   
  21. }  


Java代码
  1.   
  2. // examples   
  3.   
  4. using namespace mongo;   
  5. using namespace bson;   
  6.   
  7. bo an_obj;   
  8.   
  9. /** transform a BSON array into a vector of BSONElements.  
  10.     we match array # positions with their vector position, and ignore  
  11.     any fields with non-numeric field names.  
  12. */  
  13. vector<be> a = an_obj["x"].Array();   
  14.   
  15. be array = an_obj["x"];   
  16. assert( array.isABSONObj() );   
  17. assert( array.type() == Array );   
  18.   
  19. // Use BSON_ARRAY macro like BSON macro, but without keys   
  20. BSONArray arr = BSON_ARRAY( "hello" << 1 << BSON( "foo" << BSON_ARRAY( "bar" << "baz" << "qux" ) ) );   
  21.   
  22. // BSONArrayBuilder can be used to build arrays without the macro   
  23. BSONArrayBuilder b;   
  24. b.append(1).append(2).arr();   
  25.   
  26. /** add all elements of the object to the specified vector */  
  27. bo myarray = an_obj["x"].Obj();   
  28. vector<be> v;   
  29. myarray.elems(v);   
  30. list<be> L;   
  31. myarray.elems(L)   
  32.   
  33.   
  34. /** add all values of the object to the specified collection.  If type mismatches,  
  35.     exception.  
  36.         template <class T>  
  37.         void Vals(vector<T> &) const;  
  38.         template <class T>  
  39.         void Vals(list<T> &) const;  
  40. */  
  41.   
  42. /** add all values of the object to the specified collection.  If type mismatches, skip.  
  43.         template <class T>  
  44.         void vals(vector<T> &) const;  
  45.         template <class T>  
  46.         void vals(list<T> &) const;  
  47. */  
// 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/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值