File: working-with-bson.md

package info (click to toggle)
mongo-cxx-driver 4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 13,832 kB
  • sloc: cpp: 61,365; python: 1,436; sh: 356; xml: 253; perl: 215; makefile: 21
file content (66 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download
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
+++
date = "2016-08-15T16:11:58+05:30"
title = "Working with BSON"
[menu.main]
  identifier = 'legacy-working-with-bson'
  weight = 15
  parent="legacy"
+++

###  BSON Helper Functions

This is a non-exhaustive list of helper functions for use in the C++ stream
syntax. An exhaustive list is here:
[bsonmisc.h](https://github.com/mongodb/mongo-cxx-driver/blob/legacy/src/mongo/bson/bsonmisc.h)

Typical example of stream syntax:

```cpp
BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
```

#### GENOID
The server will add an ``_id`` automatically if it is not included explicitly.

```cpp
BSONObj p = BSON( GENOID << "name" << "Joe" << "age" << 33 );
// result is: { _id : ..., name : "Joe", age : 33 }
```

#### LT, GT, LTE, GTE, NE
less than, greater than, etc.

```cpp
BSONObj p = BSON( "age" << GT << 21 );
// result is:  { age : { $gt : 21 } }
```

#### DATENOW
Translates to current date

```cpp
BSONObj p = BSON( "created" << DATENOW );
// result is: { created : "2009-10-09 11:41:42" }
```

#### MINKEY, MAXKEY

```cpp
BSONObj p = BSON( "a" << MINKEY);
// result is: { "a" : { "$minKey" : 1 } }
```

#### OR

```cpp
OR(BSON("x" << GT << 7), BSON("y" << LT << 6))
// result is: {$or: [{x: {$gt: 7}}, {y: {$lt: 6}}]}
```

#### BSONNULL
Translates to ``null`` value (will appear in MongoDB 2.1)

```cpp
BSONObj p = BSON( "name" << "Methuselah" << "age" << BSONNULL );
// result is: { name : "Methuselah", age : null }
```