-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathbsondemo.cpp
129 lines (102 loc) · 3.98 KB
/
bsondemo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// @file bsondemo.cpp
/*
* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
Example of use of BSON from C++. Example is solely BSON, no MongoDB involved.
Requires boost (headers only).
Works as c++ "headers-only" (the parts actually exercised herein that is - some functions
require .cpp files).
To build and run:
# "../../.." is the directory mongo/src/
g++ -o bsondemo -I ../../.. bsondemo.cpp
./bsondemo
Windows: project files are available in this directory for bsondemo.cpp for use with Visual
Studio.
*/
// It is the responsibility of the mongo client consumer to ensure that any necessary windows
// headers have already been included before including the driver facade headers.
#if defined(_WIN32)
#include <winsock2.h>
#include <windows.h>
#endif
#include "mongo/client/dbclient.h"
#include <iostream>
#include <vector>
using mongo::BSONElement;
using mongo::BSONObj;
using mongo::BSONObjBuilder;
void iter(BSONObj o) {
/* iterator example */
std::cout << "\niter()\n";
for (BSONObj::iterator i(o); i.more();) {
std::cout << ' ' << i.next().toString() << '\n';
}
}
int main() {
// As of legacy-1.0-rc1-pre, you must initialize the driver in order to use
// the BSON library OID class.
mongo::client::GlobalInstance instance;
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
std::cout << "build bits: " << 8 * sizeof(char*) << '\n' << std::endl;
/* a bson object defaults on construction to { } */
BSONObj empty;
std::cout << "empty: " << empty << std::endl;
/* make a simple { _id : <generated>, when : <now>, name : 'joe', age : 33.7 } object */
{
const mongo::OID generated = mongo::OID::gen();
std::cout << "Generated an OID: " << generated << std::endl;
BSONObjBuilder b;
b.append("_id", generated);
b.append("when", mongo::jsTime());
b.append("name", "joe");
b.append("age", 33.7);
BSONObj result = b.obj();
std::cout << "json for object with _id: " << result << std::endl;
}
/* make { name : 'joe', age : 33.7 } with a more compact notation. */
BSONObj x = BSONObjBuilder().append("name", "joe").append("age", 33.7).obj();
/* convert from bson to json */
std::string json = x.toString();
std::cout << "json for x:" << json << std::endl;
/* access some fields of bson object x */
std::cout << "Some x things: " << x["name"] << ' ' << x["age"].Number() << ' ' << x.isEmpty()
<< std::endl;
/* make a bit more complex object with some nesting
{ x : 'asdf', y : true, subobj : { z : 3, q : 4 } }
*/
BSONObj y = BSON("x"
<< "asdf"
<< "y" << true << "subobj" << BSON("z" << 3 << "q" << 4));
/* print it */
std::cout << "y: " << y << std::endl;
/* reach in and get subobj.z */
std::cout << "subobj.z: " << y.getFieldDotted("subobj.z").Number() << std::endl;
/* alternate syntax: */
std::cout << "subobj.z: " << y["subobj"]["z"].Number() << std::endl;
/* fetch all *top level* elements from object y into a vector */
std::vector<BSONElement> v;
y.elems(v);
std::cout << v[0] << std::endl;
/* into an array */
std::list<BSONElement> L;
y.elems(L);
BSONObj sub = y["subobj"].Obj();
iter(y);
return 0;
}