Skip to content

Commit 5ed3265

Browse files
authored
DOCSP-48383 document bson (#1044)
* make bson page * edit code * fix spacing * capitalization * change copy * nb edits
1 parent dd67575 commit 5ed3265

File tree

2 files changed

+122
-5
lines changed

2 files changed

+122
-5
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ driver-short = "Node.js driver"
3131
mdb-server = "MongoDB Server"
3232
min-node-version = "v16.20.1"
3333
stable-api = "Stable API"
34+
language = "JavaScript"

source/data-formats/bson.txt

Lines changed: 121 additions & 5 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
6+
==============
77

88
.. default-domain:: mongodb
99

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

0 commit comments

Comments
 (0)