Docs Menu
Docs Home
/ / /
Node.js Driver
/

Work with BSON

On this page

  • Overview
  • BSON Data Types
  • Universally Unique IDs (UUIDs)
  • Create a BSON Document
  • Change a BSON Document
  • Write BSON to a File
  • Read BSON from a File
  • API Documentation

In this guide, you can learn how to create BSON documents, read BSON from a file, and write BSON to a file by using the Node.js driver.

BSON, or Binary JSON, is the data format that MongoDB uses to organize and store data. You can use BSON documents in your JavaScript application by importing the BSON package.

The code samples in this guide use the following BSON document as an example:

{
"address" : {
"street" : "Pizza St",
"zipcode" : "10003"
},
"coord" : [-73.982419, 41.579505],
"cuisine" : "Pizza",
"name" : "Mongo's Pizza"
}

BSON supports all JSON data structure types and adds support for types including dates, different size integers, ObjectId, and binary data. For a complete list of supported types, see the BSON Types page in the MongoDB Server Manual.

The Node.js driver supports UUIDs by using the BSON Binary subclass UUID. You can create a UUID object by using the UUID() constructor. The following code example generates a random UUID:

import { UUID } from 'mongodb';
const myUuid = new BSON.UUID();

You can create a BSON document by using the same notation you use to create an object in JavaScript. The Node.js driver automatically converts JavaScript objects into BSON documents when inserting them into a collection.

The following example creates a BSON document that represents the preceding sample BSON document:

const document = {
"address": {
"street": "Pizza St",
"zipcode": "10003",
},
"coord": [-73.982419, 41.579505],
"cuisine": "Pizza",
"name": "Mongo's Pizza",
}

You can modify the contents of a BSON document by using the same notation you use to modify an object in JavaScript. The following example makes three changes to the previous BSON document:

  1. Adds a new field, restaurant_id, with the value 12345

  2. Removes the cuisine field

  3. Sets the value of the name field to "Mongo's Pizza Place"

document.restaurant_id = "12345";
delete document.cuisine;
document.name = "Mongo's Pizza Place";

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:

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
await fs.writeFile('file.bson', bsonData);
console.log('BSON data written to file.bson');

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:

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
const data = await fs.readFile('file.bson');
const document = BSON.deserialize(data);
console.log(document);
{
_id: new ObjectId('67e1823d0d63bfdf87e8928e'),
address: { street: 'Pizza St', zipcode: '10003' },
coord: [ -73.982419, 41.579505 ],
cuisine: 'Pizza',
name: "Mongo's Pizza"
}

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Data Formats