Skip to content

Commit 818ca12

Browse files
committed
include bson page
1 parent c407f23 commit 818ca12

File tree

1 file changed

+153
-7
lines changed

1 file changed

+153
-7
lines changed

source/data-formats/bson.txt

Lines changed: 153 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
.. _node-bson-control:
22
.. _node-bson:
33

4-
====
5-
BSON
6-
====
4+
===================
5+
Work with BSON Data
6+
===================
77

88
.. default-domain:: mongodb
99

@@ -17,9 +17,155 @@ Overview
1717
--------
1818

1919
In this guide, you can learn how to create BSON documents, read BSON from a file,
20-
and write BSON to a file by using {+driver-short+}.
20+
and write BSON to a file by using the {+driver-short+}.
2121

2222
**BSON**, or Binary JSON, is the data format that MongoDB uses to organize
23-
and store data. This data format includes all JSON data structure types and
24-
adds support for types including dates, different size integers, ObjectIds, and
25-
binary data.
23+
and store data. You can use BSON documents in your {+language+} application
24+
by importing the BSON package.
25+
26+
The code samples in this guide use the following BSON document as an example:
27+
28+
.. code-block:: none
29+
30+
{
31+
"address" : {
32+
"street" : "Pizza St",
33+
"zipcode" : "10003"
34+
},
35+
"coord" : [-73.982419, 41.579505],
36+
"cuisine" : "Pizza",
37+
"name" : "Mongo's Pizza"
38+
}
39+
40+
.. note:: Use the {+driver-short+}'s BSON package
41+
42+
We recommend that you use the BSON package that is bundled with the driver to avoid
43+
compatibility issues with other BSON packages. You can import the {+driver-short+}'s
44+
BSON package with the following import statement:
45+
46+
.. code-block:: js
47+
48+
import { BSON } from 'mongodb';
49+
50+
BSON Data Types
51+
---------------
52+
53+
BSON supports all JSON data structure types and adds support for types including
54+
dates, different size integers, ``ObjectId``, and binary data. For a complete list of
55+
supported types, see the :manual:`BSON Types </reference/bson-types>` page in the
56+
{+mdb-server+} Manual.
57+
58+
Universally Unique IDs (UUIDs)
59+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60+
61+
The {+driver-short+} supports UUIDs by using the BSON Binary subclass ``UUID``. You
62+
can create a ``UUID`` object by using the ``UUID()`` constructor. The following code
63+
example generates a random UUID:
64+
65+
.. code-block:: javascript
66+
67+
import { UUID } from 'mongodb';
68+
69+
const myUuid = new UUID();
70+
71+
Create a BSON Document
72+
----------------------
73+
74+
You can create a BSON document by using the same notation you use to create an
75+
object in {+language+}. The {+driver-short+} automatically converts {+language+} objects
76+
into BSON documents when inserting them into a collection.
77+
78+
The following example creates a BSON document that
79+
represents the preceding sample BSON document:
80+
81+
.. code-block:: javascript
82+
83+
const document = {
84+
"address": {
85+
"street": "Pizza St",
86+
"zipcode": "10003",
87+
},
88+
"coord": [-73.982419, 41.579505],
89+
"cuisine": "Pizza",
90+
"name": "Mongo's Pizza",
91+
}
92+
93+
Change a BSON Document
94+
----------------------
95+
96+
You can modify the contents of a BSON document by using the same notation you use to modify
97+
an object in {+language+}. The following example makes three changes to the previous
98+
BSON document:
99+
100+
1. Adds a new field, ``restaurant_id``, with the value ``12345``
101+
#. Removes the ``cuisine`` field
102+
#. Sets the value of the ``name`` field to ``"Mongo's Pizza Place"``
103+
104+
.. code-block:: javascript
105+
106+
document.restaurant_id = "12345";
107+
delete document.cuisine;
108+
document.name = "Mongo's Pizza Place";
109+
110+
Write BSON to a File
111+
--------------------
112+
113+
To write BSON data to a file, import the file system module and open the output file.
114+
Then, write each document to the output file. Ensure that documents are encoded in BSON
115+
format by using the ``BSON.serialize()`` method.
116+
117+
The following example writes the sample BSON document to ``file.bson``:
118+
119+
.. code-block:: javascript
120+
121+
import fs from 'fs/promises'; // Import the file system module
122+
import { BSON } from 'mongodb'; // Import the BSON package
123+
124+
// Create a BSON object
125+
const bsonData = BSON.serialize(result);
126+
127+
// Write the BSON data to a file
128+
await fs.writeFile('file.bson', bsonData);
129+
console.log('BSON data written to file.bson');
130+
131+
Read BSON from a File
132+
---------------------
133+
134+
To read BSON documents from a file, open a file in read mode. Then, decode the documents
135+
from BSON format as you read them by using the ``BSON.deserialize()`` method.
136+
137+
The following example reads the sample BSON document from ``file.bson``:
138+
139+
.. io-code-block::
140+
:copyable: true
141+
142+
.. input::
143+
:language: javascript
144+
145+
import fs from 'fs/promises'; // Import the file system module
146+
import { BSON } from 'mongodb'; // Import the BSON package
147+
148+
// Read the BSON data from a file
149+
const data = await fs.readFile('file.bson');
150+
const document = BSON.deserialize(data);
151+
console.log(document);
152+
153+
.. output::
154+
:visible: false
155+
156+
{
157+
_id: new ObjectId('67e1823d0d63bfdf87e8928e'),
158+
address: { street: 'Pizza St', zipcode: '10003' },
159+
coord: [ -73.982419, 41.579505 ],
160+
cuisine: 'Pizza',
161+
name: "Mongo's Pizza"
162+
}
163+
164+
API Documentation
165+
-----------------
166+
167+
To learn more about any of the methods or types discussed in this
168+
guide, see the following API documentation:
169+
170+
- `BSON <{+api+}/modules/BSON.html>`__
171+
- `UUID <{+api+}/classes/BSON.UUID.html>`__

0 commit comments

Comments
 (0)