|
| 1 | +.. _csharp-bson: |
| 2 | + |
| 3 | +============== |
| 4 | +Work with BSON |
| 5 | +============== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Overview |
| 16 | +-------- |
| 17 | + |
| 18 | +In this guide, you can learn how to create BSON documents, read BSON from a file, |
| 19 | +and write BSON to a file using the {+driver-short+}. |
| 20 | + |
| 21 | +BSON Data Format |
| 22 | +---------------- |
| 23 | + |
| 24 | +**BSON**, or Binary JSON, is the data format that MongoDB uses to organize |
| 25 | +and store data. This data format includes all JSON data structure types and |
| 26 | +adds support for types including dates, different size integers, ObjectIds, and |
| 27 | +binary data. For a complete list of supported types, see the |
| 28 | +:manual:`BSON Types </reference/bson-types>` server manual page. |
| 29 | + |
| 30 | +The code samples in this guide use the following BSON document as an example: |
| 31 | + |
| 32 | +.. code-block:: none |
| 33 | + |
| 34 | + { |
| 35 | + "address" : { |
| 36 | + "street" : "Pizza St", |
| 37 | + "zipcode" : "10003" |
| 38 | + }, |
| 39 | + "coord" : [-73.982419, 41.579505] |
| 40 | + "cuisine" : "Pizza", |
| 41 | + "name" : "Mongo's Pizza" |
| 42 | + } |
| 43 | + |
| 44 | +Create a BSON Document |
| 45 | +---------------------- |
| 46 | + |
| 47 | +To build a BSON document in {+language+}, create an instance of the ``BsonDocument`` class. |
| 48 | +The ``BsonDocument`` constructor accepts ``BsonElement`` arguments that map to the fields |
| 49 | +and values in the document. Each ``BsonElement`` can be either an instance of the |
| 50 | +``BsonElement`` class or a field-value pair inside curly braces ( ``{}`` ). |
| 51 | + |
| 52 | +The following code sample shows how to create a ``BsonDocument`` object to represent the |
| 53 | +example BSON document. Each key-value pair in the ``BsonDocument`` object is a |
| 54 | +``BsonElement`` object. |
| 55 | + |
| 56 | +.. literalinclude:: ../../includes/fundamentals/code-examples/Bson.cs |
| 57 | + :start-after: start-create |
| 58 | + :end-before: newRestaurant.Add |
| 59 | + :language: csharp |
| 60 | + :copyable: |
| 61 | + :dedent: |
| 62 | + |
| 63 | +Change a BSON Document |
| 64 | +---------------------- |
| 65 | + |
| 66 | +The ``BsonDocument`` class includes methods that let you change the contents of the |
| 67 | +BSON document. The following code sample makes three changes to the previous |
| 68 | +``BsonDocument`` object: |
| 69 | + |
| 70 | +1. Adds a new field, ``"restaurant_id"``, with the value ``"12345"`` |
| 71 | +#. Removes the ``"cuisine"`` field |
| 72 | +#. Sets the value of the ``"name"`` field to ``"Mongo's Pizza Palace"`` |
| 73 | + |
| 74 | +.. literalinclude:: ../../includes/fundamentals/code-examples/Bson.cs |
| 75 | + :start-after: start-create |
| 76 | + :end-before: end-change |
| 77 | + :language: csharp |
| 78 | + :copyable: |
| 79 | + :dedent: |
| 80 | + :emphasize-lines: 15-17 |
| 81 | + |
| 82 | +.. note:: |
| 83 | + |
| 84 | + For a full list of methods in the ``BsonDocument`` class, see the |
| 85 | + :ref:`csharp-bson-api`. |
| 86 | + |
| 87 | +Write BSON to a File |
| 88 | +-------------------- |
| 89 | + |
| 90 | +You can write BSON to a file using the methods in the ``BsonBinaryWriter`` class. To |
| 91 | +write to a file, perform the following steps: |
| 92 | + |
| 93 | +1. Open a file stream for the file containing BSON data. |
| 94 | +#. Create a ``BsonBinaryWriter`` using the file stream. |
| 95 | +#. For each BSON document and subdocument you want to create, call |
| 96 | + ``WriteStartDocument()``. |
| 97 | +#. Within each BSON document and subdocument, call ``WriteName()`` to set the field |
| 98 | + name and the appropriate ``Write*`` method to set its value. Each data type has a |
| 99 | + dedicated ``Write*`` method that you should use. |
| 100 | +#. To start and end arrays, use ``WriteStartArray()`` and ``WriteEndArray()``. |
| 101 | +#. At the end of each document and subdocument, call ``WriteEndDocument()``. |
| 102 | + |
| 103 | +The following code sample shows how to write the sample BSON document to ``myFile.bson``: |
| 104 | + |
| 105 | +.. code-block:: csharp |
| 106 | + |
| 107 | + string outputFileName = "myFile.bson"; |
| 108 | + |
| 109 | + using (var stream = File.OpenWrite(outputFileName)) |
| 110 | + using (var writer = new BsonBinaryWriter(stream)) |
| 111 | + { |
| 112 | + writer.WriteStartDocument(); |
| 113 | + |
| 114 | + //address |
| 115 | + writer.WriteName("address"); |
| 116 | + writer.WriteStartDocument(); |
| 117 | + writer.WriteName("street"); |
| 118 | + writer.WriteString("Pizza St"); |
| 119 | + writer.WriteName("zipcode"); |
| 120 | + writer.WriteString("10003"); |
| 121 | + writer.WriteEndDocument(); |
| 122 | + |
| 123 | + //coord |
| 124 | + writer.WriteName("coord"); |
| 125 | + writer.WriteStartArray(); |
| 126 | + writer.WriteDouble(-73.982419); |
| 127 | + writer.WriteDouble(41.579505); |
| 128 | + writer.WriteEndArray(); |
| 129 | + |
| 130 | + //cuisine |
| 131 | + writer.WriteName("cuisine"); |
| 132 | + writer.WriteString("Pizza"); |
| 133 | + |
| 134 | + //name |
| 135 | + writer.WriteName("name"); |
| 136 | + writer.WriteString("Mongo's Pizza"); |
| 137 | + |
| 138 | + writer.WriteEndDocument(); |
| 139 | + } |
| 140 | + |
| 141 | +The resulting BSON document looks like the following: |
| 142 | + |
| 143 | +.. code-block:: none |
| 144 | + |
| 145 | + { |
| 146 | + "address" : { |
| 147 | + "street" : "Pizza St", |
| 148 | + "zipcode" : "10003" |
| 149 | + }, |
| 150 | + "coord" : [-73.982419, 41.579505] |
| 151 | + "cuisine" : "Pizza", |
| 152 | + "name" : "Mongo's Pizza" |
| 153 | + } |
| 154 | + |
| 155 | +Read BSON from a File |
| 156 | +--------------------- |
| 157 | + |
| 158 | +To read a BSON document from a file, follow the same steps used for writing a BSON |
| 159 | +document to a file, with two differences: |
| 160 | + |
| 161 | +- Use ``BsonBinaryReader`` instead of ``BsonBinaryWriter``. |
| 162 | +- Use ``Read*`` methods instead of ``Write*`` methods. These methods return field names |
| 163 | + and values from the BSON document. |
| 164 | + |
| 165 | +The following code sample shows how to read the fields and values from the sample |
| 166 | +BSON document stored in ``myFile.bson``: |
| 167 | + |
| 168 | +.. code-block:: csharp |
| 169 | + |
| 170 | + string inputFileName = "myFile.bson"; |
| 171 | + |
| 172 | + using (var stream = File.OpenRead(inputFileName)) |
| 173 | + using (var reader = new BsonBinaryReader(stream)) |
| 174 | + { |
| 175 | + reader.ReadStartDocument(); |
| 176 | + |
| 177 | + //address |
| 178 | + string addressFieldName = reader.ReadName(); |
| 179 | + reader.ReadStartDocument(); |
| 180 | + string streetFieldName = reader.ReadName(); |
| 181 | + string streetValue = reader.ReadString(); |
| 182 | + string zipFieldName = reader.ReadName(); |
| 183 | + string zipValue = reader.ReadString(); |
| 184 | + reader.ReadEndDocument(); |
| 185 | + |
| 186 | + //coord |
| 187 | + string coordFieldName = reader.ReadName(); |
| 188 | + reader.ReadStartArray(); |
| 189 | + double coord1 = reader.ReadDouble(); |
| 190 | + double coord2 = reader.ReadDouble(); |
| 191 | + reader.ReadEndArray(); |
| 192 | + |
| 193 | + //cuisine |
| 194 | + string cuisineFieldName = reader.ReadName(); |
| 195 | + string cuisineValue = reader.ReadString(); |
| 196 | + |
| 197 | + //name |
| 198 | + string nameFieldName = reader.ReadName(); |
| 199 | + string nameValue = reader.ReadString(); |
| 200 | + |
| 201 | + reader.ReadEndDocument(); |
| 202 | + } |
| 203 | + |
| 204 | +.. warning:: |
| 205 | + |
| 206 | + If you call ``ReadName()`` twice in a row without reading a value, |
| 207 | + the driver will throw an ``InvalidOperationException``. |
| 208 | + |
| 209 | +.. tip:: |
| 210 | + |
| 211 | + The ``BsonBinaryReader`` and ``BsonBinaryWriter`` constructors accept any |
| 212 | + ``System.IO.Stream`` object. This means that you can read or write any location |
| 213 | + that can be accessed by a stream. |
| 214 | + |
| 215 | +.. _csharp-bson-api: |
| 216 | + |
| 217 | +API Documentation |
| 218 | +----------------- |
| 219 | + |
| 220 | +To learn more about any of the methods or types discussed in this |
| 221 | +guide, see the following API documentation: |
| 222 | + |
| 223 | +- `BsonDocument <{+api-root+}/T_MongoDB_Bson_BsonDocument.htm>`__ |
| 224 | +- `BsonElement <{+api-root+}/T_MongoDB_Bson_BsonElement.htm>`__ |
| 225 | +- `BsonBinaryReader <{+api-root+}/T_MongoDB_Bson_IO_BsonBinaryReader.htm>`__ |
| 226 | +- `BsonBinaryWriter <{+api-root+}/T_MongoDB_Bson_IO_BsonBinaryWriter.htm>`__ |
0 commit comments