-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
71 lines (69 loc) · 1.46 KB
/
test.cpp
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
64
65
66
67
68
69
70
71
#include "Mongo.hpp"
#include <thread>
#include <stdio.h>
#include <memory>
#include <time.h>
void test(const char* dbconnection,const char* dbname)
{
auto pool = std::make_shared<nicehero::MongoConnectionPool>();
if (!pool->init(dbconnection,dbname))
{
printf("error db %s\n",dbconnection);
return;
}
auto client = pool->popClient();
if (!client)
{
return;
}
std::thread t([client]{
//insert
client->insert("easy",
NBSON_T(
"_id", BCON_INT64(100)
, "hello", BCON_UTF8("world")
, "ar"
, "["
, "{"
, "hello", BCON_INT64(666)
, "}"
, "world5"
, BCON_DATE_TIME(time(0) * 1000)
, "]"
, "oo"
, "{"
,"xhello", BCON_INT64(666)
, "}"
));
//update
auto obj = NBSON("$set", "{", "ar.0.hello", BCON_INT64(101), "}");
client->update("easy",
NBSON_T("_id", BCON_INT64(100)),
*obj);
//find
auto cursor = client->find("easy", NBSON_T("_id", BCON_INT64(100)), nicehero::Bson(nullptr));
while (auto r = cursor->fetch())
{
if (r->isInt64("oo.xhello"))
{
printf("oo.xhello: %d\n", int(r->asInt64("oo.xhello")));
}
if (r->isInt64("ar.0.hello"))
{
printf("ar.0.hello: %d\n", int(r->asInt64("ar.0.hello")));
}
}
});
t.join();
}
int main(int argc, char* argv[])
{
if (argc < 3)
{
printf("need param: ./test dbconnection dbname\n");
printf("example: ./test mongodb://user:pass@localhost:27017/?authSource=admin dbname\n");
return 0;
}
test(argv[1], argv[2]);
return 0;
}