-
Notifications
You must be signed in to change notification settings - Fork 51
DOCSP-48383 document bson #1044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
.. _node-bson-control: | ||
.. _node-bson: | ||
|
||
==== | ||
BSON | ||
==== | ||
============== | ||
Work with BSON | ||
============== | ||
|
||
.. default-domain:: mongodb | ||
|
||
|
@@ -17,9 +17,129 @@ 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 | ||
adds support for types including dates, different size integers, ObjectIds, and | ||
binary data. | ||
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 </reference/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+}. The {+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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe my Node is just weak, but isn't this immutable? This may be a question for tech review, but there must me a more general way to create a BSON-structure object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little confused at what you mean by immutable in this case! When I tested this code I was able to change the document created using the code in the "change a bson document" section of this doc, and to my knowledge generally objects are mutable in Node JS. I also don't know much about Node or JS, but I was basing this doc off the PyMongo BSON Doc and this Stack Overflow thread. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 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 | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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'); | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
Read BSON from a File | ||
--------------------- | ||
|
||
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``: | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: | ||
:language: javascript | ||
|
||
const fs = require('fs'); // Import the file system module | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
{ | ||
_id: new ObjectId('67e1823d0d63bfdf87e8928e'), | ||
"address": {"street": "Pizza St", "zipcode": "10003"}, | ||
"coord": [-73.982419, 41.579505], | ||
"cuisine": "Pizza", | ||
"name": "Mongo's Pizza" | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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. | ||
|
Uh oh!
There was an error while loading. Please reload this page.