Docs Home / Node.js Driver

Work with BSON Data使用BSON数据

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 the Node.js driver.在本指南中,您可以学习如何使用Node.js驱动程序创建BSON文档、从文件读取BSON以及将BSON写入文件。

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.BSON或二进制JSON是MongoDB用于组织和存储数据的数据格式。您可以通过导入BSON包在JavaScript应用程序中使用BSON文档。

The code samples in this guide use the following BSON document as an example:本指南中的代码示例使用以下BSON文档作为示例:

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

Note

Use the Node.js driver's BSON package使用Node.js驱动程序的BSON包

We recommend that you use the BSON package that is bundled with the driver to avoid compatibility issues with other BSON packages. You can import the Node.js driver's BSON package with the following import statement:我们建议您使用与驱动程序捆绑在一起的BSON包,以避免与其他BSON包的兼容性问题。您可以使用以下import语句导入Node.js驱动程序的BSON包:

import { BSON } from 'mongodb';

BSON Data TypesBSON数据类型

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.BSON支持所有JSON数据结构类型,并添加了对包括日期、不同大小整数、ObjectId和二进制数据在内的类型的支持。有关支持类型的完整列表,请参阅MongoDB服务器手册中的BSON类型页面。

Universally Unique IDs (UUIDs)通用唯一ID(UUID)

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:Node.js驱动程序通过使用BSON Binary子类UUID来支持UUID。您可以使用UUID()构造函数创建UUID对象。以下代码示例生成随机UUID:

import { UUID } from 'mongodb';

const myUuid = new UUID();

Create a BSON Document创建BSON文档

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.您可以使用与在JavaScript中创建对象相同的符号来创建BSON文档。Node.js驱动程序在将JavaScript对象插入集合时会自动将其转换为BSON文档。

The following example creates a BSON document that represents the preceding sample BSON document:以下示例创建了一个BSON文档,该文档表示前面的示例BSON文档:

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

Change a BSON Document更改BSON文档

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:您可以使用与在JavaScript中修改对象相同的符号来修改BSON文档的内容。以下示例对之前的BSON文档进行了三处修改:

  1. Adds a new field, restaurant_id, with the value 12345添加一个新字段restaurant_id,其值为12345
  2. Removes the cuisine field删除cuisine(烹饪)字段
  3. Sets the value of the name field to "Mongo's Pizza Place"name字段的值设置为"Mongo's Pizza Place"
document.restaurant_id = "12345";
delete document.cuisine;
document.name = "Mongo's Pizza Place";

Write BSON to a File将BSON写入文件

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.要将BSON数据写入文件,请导入文件系统模块并打开输出文件。然后,将每个文档写入输出文件。使用BSON.serialize()方法确保文档以BSON格式编码。

The following example writes the sample BSON document to file.bson:以下示例将示例BSON文档写入file.bson

import fs from 'fs/promises';  // Import the file system module导入文件系统模块
import { BSON } from 'mongodb'; // Import the BSON package导入BSON包

// Create a BSON object创建BSON对象
const bsonData = BSON.serialize(result);

// Write the BSON data to a file将BSON数据写入文件
await fs.writeFile('file.bson', bsonData);
console.log('BSON data written to file.bson');

Read BSON from a 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.要从文件中读取BSON文档,请以读取模式打开文件。然后,在读取文档时,使用BSON.deserialize()方法从BSON格式解码文档。

The following example reads the sample BSON document from file.bson:以下示例从file.bson读取示例BSON文档:

import fs from 'fs/promises';  // Import the file system module导入文件系统模块
import { BSON } from 'mongodb'; // Import the BSON package导入BSON包

// Read the BSON data from a file从文件中读取BSON数据
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"
}

API Documentation文档

To learn more about any of the methods or types discussed in this guide, see the following API documentation:要了解有关本指南中讨论的任何方法或类型的更多信息,请参阅以下API文档: