-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_subarr_subdoc.cpp
65 lines (54 loc) · 2.11 KB
/
insert_subarr_subdoc.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
#include <iostream>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/types.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/exception/exception.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/pool.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/document/element.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/types.hpp>
#include <bsoncxx/builder/concatenate.hpp>
using namespace::bsoncxx;
using bsoncxx::builder::basic::kvp;
using builder::stream::open_document;
using builder::stream::close_document;
using bsoncxx::builder::concatenate;
int main(int, char**) {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
// must remain alive for as long as the driver is in use.
mongocxx::instance inst{};
mongocxx::uri uri = mongocxx::uri("mongodb://");
mongocxx::pool *_pMongoPoolptr = new mongocxx::pool(uri);
mongocxx::pool::entry pentry = _pMongoPoolptr->acquire();
mongocxx::client & client = *pentry;
mongocxx::collection coll = client["test"]["testCollection"];
try {
bsoncxx::builder::basic::document doc,docfilter;
bsoncxx::document::value q(doc.view());
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::sub_document;
using bsoncxx::builder::basic::sub_array;
doc.append(kvp("code", "0101600000"), kvp("date", 20190819), kvp("update", 20190819));
doc.append(kvp("dataArray", [](sub_array subarr){
subarr.append(
[](sub_document subdoc){
subdoc.append(kvp("fieldname", "yingyeting"), kvp("value", "shanghai"));
});
subarr.append(
[](sub_document subdoc){
subdoc.append(kvp("fieldname", "yingyee"), kvp("value", 1234354.90));});
}) );
std::cout << bsoncxx::to_json(doc.view()) << std::endl;
//2
coll.insert_one(doc.view());
} catch (const mongocxx::exception& e) {
std::cout << "An exception occurred: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}