From ed474724ee240d00d126162d5226a99d57b25575 Mon Sep 17 00:00:00 2001 From: Angela Date: Mon, 24 Mar 2025 10:21:58 -0400 Subject: [PATCH 1/6] make bson page --- snooty.toml | 1 + source/data-formats/bson.txt | 78 +++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/snooty.toml b/snooty.toml index 4c887b90d..b042aa5c6 100644 --- a/snooty.toml +++ b/snooty.toml @@ -31,3 +31,4 @@ driver-short = "Node.js driver" mdb-server = "MongoDB Server" min-node-version = "v16.20.1" stable-api = "Stable API" +language = "Javascript" diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 2e76a802b..5907ef52d 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -22,4 +22,80 @@ and write BSON to a file by using {+driver-short+}. **BSON**, or Binary JSON, is the data format that MongoDB uses to organize and store data. This data format includes all JSON data structure types and adds support for types including dates, different size integers, ObjectIds, and -binary data. \ No newline at end of file +binary data. You can use BSON documents in your {+language+} application by importing the +BSON package. For a complete list of supported types, see the +:manual:`BSON Types ` server manual page. + +The code samples in this guide use the following BSON document as an example: + +.. code-block:: none + + { + "address" : { + "street" : "Pizza St", + "zipcode" : "10003" + }, + "coord" : [-73.982419, 41.579505], + "cuisine" : "Pizza", + "name" : "Mongo's Pizza" + } + +Create a BSON Document +---------------------- + +You can create a BSON document by using the same notation you use to create an +object in {+language+}. {+driver-short+} automatically converts {+language+} objects +into BSON documents when inserting them into a collection. + +The following example creates a BSON document that +represents the preceding sample BSON document: + +.. code-block:: javascript + + const document = { + "address": { + "street": "Pizza St", + "zipcode": "10003", + }, + "coord": [-73.982419, 41.579505], + "cuisine": "Pizza", + "name": "Mongo's Pizza", + } + +Change a BSON Document +---------------------- + +You can modify the contents of a BSON document by using the same notation you use to modify +an object in {+language+}. The following example makes three changes to the previous +BSON document: + +1. Adds a new field, ``restaurant_id``, with the value ``12345`` +#. Removes the ``cuisine`` field +#. Sets the value of the ``name`` field to ``"Mongo's Pizza Place"`` + +.. code-block:: javascript + + document.restaurant_id = "12345" + delete document.cuisine + document.name = "Mongo's Pizza Place" + +Write BSON to a File +-------------------- + +To write BSON data to a file, import the file system module and open a file stream on the output file. +Then, write each document to the output file. Ensure that documents are encoded in BSON +format by using the ``bson.serialize()`` method. + +The following example writes the sample BSON document to ``file.bson``: + +.. code-block:: javascript + +const fs = require('fs'); // Import the file system module +const BSON = require('bson'); // Import the BSON package + +// Create a BSON object +const bson = new BSON(); + +// Write the BSON data to a file +fs.writeFile('file.bson', bson.serialize(document)); + From 726f6944efa00a589854124c49c6e03726df89fa Mon Sep 17 00:00:00 2001 From: Angela Date: Mon, 24 Mar 2025 12:38:29 -0400 Subject: [PATCH 2/6] edit code --- source/data-formats/bson.txt | 68 ++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 5907ef52d..f7414f0ae 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -1,9 +1,9 @@ .. _node-bson-control: .. _node-bson: -==== -BSON -==== +============== +Work with BSON +============== .. default-domain:: mongodb @@ -75,9 +75,9 @@ BSON document: .. code-block:: javascript - document.restaurant_id = "12345" - delete document.cuisine - document.name = "Mongo's Pizza Place" + document.restaurant_id = "12345"; + delete document.cuisine; + document.name = "Mongo's Pizza Place"; Write BSON to a File -------------------- @@ -90,12 +90,52 @@ The following example writes the sample BSON document to ``file.bson``: .. code-block:: javascript -const fs = require('fs'); // Import the file system module -const BSON = require('bson'); // Import the BSON package - -// Create a BSON object -const bson = new BSON(); - -// Write the BSON data to a file -fs.writeFile('file.bson', bson.serialize(document)); + const fs = require('fs'); // Import the file system module + const BSON = require('bson'); // Import the BSON package + + // Create a BSON object + const bsonData = BSON.serialize(result); + + // Write the BSON data to a file + fs.writeFile('file.bson', bsonData, (err) => { + if (err) throw err; + console.log('BSON data written to file.bson'); + }); + +Read BSON from a File +--------------------- + +To read BSON documents from a file, open a file stream in read-binary mode on the input +file. Then, decode the documents from BSON format as you read them by using the ``bson.decode()`` +method. + +The following example reads the sample BSON document from ``file.bson``: + +.. io-code-block:: + :copyable: true + + .. input:: + :language: javascript + + const fs = require('fs'); // Import the file system module + const BSON = require('bson'); // Import the BSON package + + // Read the BSON data from a file + fs.readFile('file.bson', (err, data) => { + if (err) throw err; + const document = BSON.deserialize(data); + console.log(document); + }); + + .. output:: + :visible: false + {_id: new ObjectId('67e1823d0d63bfdf87e8928e'), "address": {"street": "Pizza St", + "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": + "Mongo's Pizza"} + +API Documentation +----------------- + +To learn more about any of the methods or types discussed in this +guide, see the `bson <{+api+}/modules/BSON.html>`__ API documentation. From ffc3072ac22553a1f918c64674928e66e8479ce0 Mon Sep 17 00:00:00 2001 From: Angela Date: Mon, 24 Mar 2025 13:26:28 -0400 Subject: [PATCH 3/6] fix spacing --- source/data-formats/bson.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index f7414f0ae..fbb95ceeb 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -129,6 +129,7 @@ The following example reads the sample BSON document from ``file.bson``: .. output:: :visible: false + {_id: new ObjectId('67e1823d0d63bfdf87e8928e'), "address": {"street": "Pizza St", "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": "Mongo's Pizza"} From 2fa207de8f86602eb10c3173e7276740cd60f0de Mon Sep 17 00:00:00 2001 From: Angela Date: Mon, 24 Mar 2025 13:26:58 -0400 Subject: [PATCH 4/6] capitalization --- source/data-formats/bson.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index fbb95ceeb..7416136a7 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -129,7 +129,7 @@ The following example reads the sample BSON document from ``file.bson``: .. output:: :visible: false - + {_id: new ObjectId('67e1823d0d63bfdf87e8928e'), "address": {"street": "Pizza St", "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": "Mongo's Pizza"} @@ -138,5 +138,5 @@ API Documentation ----------------- To learn more about any of the methods or types discussed in this -guide, see the `bson <{+api+}/modules/BSON.html>`__ API documentation. +guide, see the `BSON <{+api+}/modules/BSON.html>`__ API documentation. From 4b446943be76548b5db7f2124deefe6fd14f1d0a Mon Sep 17 00:00:00 2001 From: Angela Date: Mon, 24 Mar 2025 13:58:10 -0400 Subject: [PATCH 5/6] change copy --- source/data-formats/bson.txt | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 7416136a7..6bd626aee 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -17,7 +17,7 @@ Overview -------- In this guide, you can learn how to create BSON documents, read BSON from a file, -and write BSON to a file by using {+driver-short+}. +and write BSON to a file by using the {+driver-short+}. **BSON**, or Binary JSON, is the data format that MongoDB uses to organize and store data. This data format includes all JSON data structure types and @@ -44,7 +44,7 @@ Create a BSON Document ---------------------- You can create a BSON document by using the same notation you use to create an -object in {+language+}. {+driver-short+} automatically converts {+language+} objects +object in {+language+}. The {+driver-short+} automatically converts {+language+} objects into BSON documents when inserting them into a collection. The following example creates a BSON document that @@ -82,9 +82,9 @@ BSON document: Write BSON to a File -------------------- -To write BSON data to a file, import the file system module and open a file stream on the output file. +To write BSON data to a file, import the file system module and open the output file. Then, write each document to the output file. Ensure that documents are encoded in BSON -format by using the ``bson.serialize()`` method. +format by using the ``BSON.serialize()`` method. The following example writes the sample BSON document to ``file.bson``: @@ -105,9 +105,8 @@ The following example writes the sample BSON document to ``file.bson``: Read BSON from a File --------------------- -To read BSON documents from a file, open a file stream in read-binary mode on the input -file. Then, decode the documents from BSON format as you read them by using the ``bson.decode()`` -method. +To read BSON documents from a file, open a file in read mode. Then, decode the documents +from BSON format as you read them by using the ``BSON.deserialize()`` method. The following example reads the sample BSON document from ``file.bson``: @@ -130,9 +129,13 @@ The following example reads the sample BSON document from ``file.bson``: .. output:: :visible: false - {_id: new ObjectId('67e1823d0d63bfdf87e8928e'), "address": {"street": "Pizza St", - "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": - "Mongo's Pizza"} + { + _id: new ObjectId('67e1823d0d63bfdf87e8928e'), + "address": {"street": "Pizza St", "zipcode": "10003"}, + "coord": [-73.982419, 41.579505], + "cuisine": "Pizza", + "name": "Mongo's Pizza" + } API Documentation ----------------- From 6087262fe274b048ed502ba4116d3e542107e081 Mon Sep 17 00:00:00 2001 From: Angela Date: Mon, 24 Mar 2025 17:04:52 -0400 Subject: [PATCH 6/6] nb edits --- snooty.toml | 2 +- source/data-formats/bson.txt | 32 ++++++++++++++------------------ 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/snooty.toml b/snooty.toml index b042aa5c6..d9ec0034d 100644 --- a/snooty.toml +++ b/snooty.toml @@ -31,4 +31,4 @@ driver-short = "Node.js driver" mdb-server = "MongoDB Server" min-node-version = "v16.20.1" stable-api = "Stable API" -language = "Javascript" +language = "JavaScript" diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 6bd626aee..25fd0f948 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -90,17 +90,15 @@ The following example writes the sample BSON document to ``file.bson``: .. code-block:: javascript - const fs = require('fs'); // Import the file system module - const BSON = require('bson'); // Import the BSON package + import fs from 'fs/promises'; // Import the file system module + import { BSON } from 'bson'; // Import the BSON package // Create a BSON object const bsonData = BSON.serialize(result); // Write the BSON data to a file - fs.writeFile('file.bson', bsonData, (err) => { - if (err) throw err; - console.log('BSON data written to file.bson'); - }); + await fs.writeFile('file.bson', bsonData); + console.log('BSON data written to file.bson'); Read BSON from a File --------------------- @@ -116,25 +114,23 @@ The following example reads the sample BSON document from ``file.bson``: .. input:: :language: javascript - const fs = require('fs'); // Import the file system module - const BSON = require('bson'); // Import the BSON package + import fs from 'fs/promises'; // Import the file system module + import { BSON } from 'bson'; // Import the BSON package // Read the BSON data from a file - fs.readFile('file.bson', (err, data) => { - if (err) throw err; - const document = BSON.deserialize(data); - console.log(document); - }); + const data = await fs.readFile('file.bson'); + const document = BSON.deserialize(data); + console.log(document); .. output:: :visible: false { - _id: new ObjectId('67e1823d0d63bfdf87e8928e'), - "address": {"street": "Pizza St", "zipcode": "10003"}, - "coord": [-73.982419, 41.579505], - "cuisine": "Pizza", - "name": "Mongo's Pizza" + _id: new ObjectId('67e1823d0d63bfdf87e8928e'), + address: { street: 'Pizza St', zipcode: '10003' }, + coord: [ -73.982419, 41.579505 ], + cuisine: 'Pizza', + name: "Mongo's Pizza" } API Documentation