-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb_conn.cc
63 lines (56 loc) · 2 KB
/
mongodb_conn.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Copyright (c) 2017-2018 SYZ
* https://github.com/mongodb/mongo-cxx-driver
*
* g++ mongodb_conn.cc -std=c++11 -I/usr/local/include/libmongoc-1.0
* -I/usr/local/mongo-cxx-driver/include/mongocxx/v_noabi
* -I/usr/local/mongo-cxx-driver/include/bsoncxx/v_noabi
* -I/usr/local/include/libbson-1.0 -lbsoncxx -lmongocxx -o
* release/mongodb_conn_cc
*
*/
#include <stdio.h>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/builder/stream/array.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <iostream>
#include <mongocxx/client.hpp>
#include <mongocxx/cursor.hpp>
#include <mongocxx/exception/query_exception.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/options/find.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
int main(int argc, char* argv[]) {
int result = -1;
do {
try {
mongocxx::uri uri_("mongodb://192.168.1.100:27017");
mongocxx::instance inst_;
mongocxx::client client_;
mongocxx::database db_;
client_ = mongocxx::client(uri_);
db_ = client_["local"];
mongocxx::options::find opts;
bsoncxx::builder::stream::document order_builder;
order_builder << "id_" << -1;
opts.sort(order_builder.view());
bsoncxx::builder::stream::document filter;
filter << "pid" << bsoncxx::builder::stream::open_document << "$gt"
<< 10000 << bsoncxx::builder::stream::close_document;
filter << "pid" << bsoncxx::builder::stream::open_document << "$lt"
<< 20000 << bsoncxx::builder::stream::close_document;
mongocxx::cursor cursor_ = db_["startup_log"].find(filter.view(), opts);
mongocxx::cursor::iterator it = cursor_.begin();
for (; it != cursor_.end(); ++it) {
bsoncxx::document::view view = *it;
bsoncxx::document::element element{view["pid"]};
printf("%lu\n", element.get_int64().value);
}
result = 0;
} catch (mongocxx::query_exception e) {
std::cout << "failed" << std::endl;
}
} while (0);
return result;
}