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/
以下是一个基本的MongoDB C++ driver异步操作代码示例,包括回调函数处理结果: ```c++ #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/uri.hpp> using namespace mongocxx; // 回调函数,处理异步查询结果 void async_query_callback(mongocxx::cursor::iterator it, mongocxx::query::iterator /*query_it*/, const boost::optional<mongocxx::read_concern>& /*read_concern*/, const boost::optional<mongocxx::read_preference>& /*read_preference*/, mongocxx::cursor::id /*cursor_id*/, const mongocxx::operation_duration& /*duration*/, const mongocxx::stdx::optional<bsoncxx::document::value>& /*command_reply*/, mongocxx::stdx::string_view /*database*/, mongocxx::stdx::string_view /*collection*/, std::chrono::system_clock::time_point /*start_time*/) { if (it != mongocxx::cursor::end(it)) { // 处理查询结果 bsoncxx::document::view doc = *it; std::cout << bsoncxx::to_json(doc) << std::endl; } else { std::cout << "No results found." << std::endl; } } int main() { // 初始化MongoDB C++ driver mongocxx::instance instance{}; // 创建MongoDB客户端 mongocxx::uri uri("mongodb://localhost:27017"); mongocxx::client client(uri); // 创建异步查询对象 mongocxx::database db = client["mydb"]; mongocxx::collection coll = db["mycoll"]; mongocxx::options::find opts; bsoncxx::document::view_or_value filter = bsoncxx::builder::stream::document{} << "name" << "John" << finalize; // 异步查询 mongocxx::stdx::function<void(mongocxx::cursor::iterator, mongocxx::query::iterator, const boost::optional<mongocxx::read_concern>&, const boost::optional<mongocxx::read_preference>&, mongocxx::cursor::id, const mongocxx::operation_duration&, const mongocxx::stdx::optional<bsoncxx::document::value>&, mongocxx::stdx::string_view, mongocxx::stdx::string_view, std::chrono::system_clock::time_point)> callback = async_query_callback; coll.find(filter, opts).subscribe(callback); // 等待异步查询完成 std::this_thread::sleep_for(std::chrono::seconds(1)); return 0; } ``` 在这个示例中,我们首先创建了一个MongoDB客户端对象,并使用它来获取一个数据库和一个集合对象。然后,我们创建了一个包含查询条件的BSON文档,并将其作为参数传递给集合的`find`方法。该方法返回一个异步查询对象,我们使用`subscribe`方法来订阅该对象的结果,并将回调函数`async_query_callback`作为参数传递给它。该回调函数将在异步查询完成后被调用,并将查询结果作为参数传递给它。在这个示例中,我们简单地将查询结果打印到控制台上。 需要注意的是,在异步查询完成之前,我们需要阻止程序的执行,以便查询有足够的时间完成。在这个示例中,我们使用了一个简单的`std::this_thread::sleep_for`调用来等待1秒钟,以确保异步查询已完成。在实际应用中,我们可能需要使用更复杂的方法来等待异步查询完成,例如使用条件变量或使用异步编程框架。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值