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 130 131 132
|
// Copyright 2009-present MongoDB, 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.
#include <chrono>
#include <bsoncxx/builder/list.hpp>
#include <bsoncxx/types.hpp>
#include <bsoncxx/types/bson_value/value.hpp>
#include <examples/macros.hh>
using namespace bsoncxx;
int EXAMPLES_CDECL main() {
using namespace bsoncxx::builder;
//
// bsoncxx::builder::list, bsoncxx::builder::document, and bsoncxx::builder::array provides a
// JSON-like interface for creating BSON objects.
//
// builder::document builds an empty BSON document
builder::document doc = {};
// builder::array builds an empty BSON array
builder::array arr = {};
//
// We can append values to a document using an initializer list of key-value pairs.
// { "hello" : "world" }
//
doc = {"hello", "world"};
//
// Each document key must be a string type.
//
// {
// "c-style" : "with value",
// "basic string" : "with value"
// }
//
doc = {"c-style", "with value", std::string("basic string"), "with value"};
//
// Each document value must be a bson_value::value or implicitly convertible to one.
//
// {
// "BSON boolean value" : false,
// "BSON 32-bit signed integer value" : -123,
// "BSON date value" : { "$date" : 123456789 },
// "BSON Decimal128 value" : { "$numberDecimal" : "1.844674407370955161800E-6155" },
// "BSON regex value with options" : { "$regex" : "any", "$options" : "imsx" }
// }
//
// clang-format off
doc = {"BSON boolean value", false,
"BSON 32-bit signed integer value", -123,
"BSON date value", std::chrono::milliseconds(123456789),
"BSON Decimal128 value", decimal128{100, 200},
"BSON regex value with options", types::bson_value::value("regex", "imsx" /* opts */)};
// clang-format on
//
// Nested documents can be added in-place.
//
// { "Answers" :
// { "Everything" :
// { "The Universe" : { "Life" : 42 } }
// }
// }
//
doc = {"Answers", {"Everything", {"The Universe", {"Life", 42}}}};
//
// Each array element must be bson_value::value or implicitly convertible to one.
//
// { "0" : false,
// "1" : -123,
// "2" : { "$date" : 123456789 },
// "3" : { "$numberDecimal" : "1.844674407370955161800E-6155" },
// "4" : { "$regex" : "any", "$options" : "imsx" }
// }
//
arr = {false,
-123,
std::chrono::milliseconds(123456789),
decimal128{100, 200},
types::bson_value::value("regex", "imsx" /* opts */)};
//
// The list builder will create a BSON document, if possible. Otherwise, it will create a BSON
// array. A document is possible if:
// 1. The initializer list's size is even; this implies a list of
// key-value pairs or an empty document if the size is zero.
// 2. Each 'key' is a string type. In a list of key-value pairs, the 'key' is every other
// element starting at the 0th element.
//
// BSON document
// { "pi" : 3.1415899999999998826, "e" : 2.7182800000000000296 }
builder::list lst = {"pi", 3.14159, "e", 2.71828};
// BSON array
// { "0" : "Numbers", "1" : 3.1415899999999998826, "2" : 2.7182800000000000296 }
lst = {"Numbers", 3.14159, 2.71828};
//
// BSON document
//
// { "this" : { "is" : 10 },
// "valid" : { "BSON" : "document" }
// "with" : [ 1, "nested", "array" ] }
lst = {"this", {"is", 0xA}, "valid", {"BSON", "document"}, "with", {1, "nested", "array"}};
//
// BSON array
//
// { "0" : "this",
// "1" : { "will" : "be" },
// "2" : "an",
// "3" : "array",
// "4" : [ 1, 2, 3] }
lst = {"this", {"will", "be"}, "an", "array", {1, 2, 3}};
}
|