Docs Home / mongosh / Reference / EJSON

EJSON.serialize()

The EJSON.serialize() method converts BSON objects to Extended JSON representation as JavaScript objects.EJSON.serialize()方法将BSON对象转换为JavaScript对象的扩展JSON表示。

MongoDB stores data using BSON. Many external data transformation applications use JSON. You can use EJSON.serialize() to convert BSON to JSON and save the output for those external applications.MongoDB使用BSON存储数据。许多外部数据转换应用程序使用JSON。您可以使用EJSON.serialize()将BSON转换为JSON,并为这些外部应用程序保存输出。

Syntax语法

The method has this syntax:该方法具有以下语法:

EJSON.serialize( object, [ options ] )

Method Fields方法字段

The method takes the following fields:该方法接受以下字段:

Field字段Type类型Necessity必要性Description描述
objectBSON objectRequired必需BSON object to convert. For example, an array of documents.要转换的BSON对象。例如,一系列文档。
optionsstringOptional可选

Modifies the output object types. The only option is { relaxed: <boolean> }.修改输出对象类型。唯一的选择是{ relaxed: <boolean> }

Boolean Value布尔值Description描述

true

Return JSON object types. Default is true.返回JSON对象类型。默认值为true
falseReturn BSON object types.返回BSON对象类型。

Behavior行为

You can run EJSON.serialize() from an interactive mongosh session or from the system command line using --eval.您可以从交互式mongosh会话或使用--eval从系统命令行运行EJSON.serialize()

To run EJSON.serialize() from an interactive mongosh session, use:要从交互式mongosh会话运行EJSON.serialize(),请使用:

EJSON.serialize( object, [ options ] )

To run EJSON.serialize() from the system command line, use:要从系统命令行运行EJSON.serialize(),请使用:

mongosh --eval "EJSON.serialize( object, [ options ] )"

Examples示例

Create the sales collection for the examples:为以下示例创建sales集合:

db.sales.insertMany( [
{ custId: 345, purchaseDate: ISODate("2023-07-04"),
quantity: 4, cost: Decimal128("100.60") },
{ custId: 346, purchaseDate: ISODate("2023-07-12"),
quantity: 3, cost: Decimal128("175.45") },
{ custId: 486, purchaseDate: ISODate("2023-08-01"),
quantity: 9, cost: Decimal128("200.53") }
] )

Interactive Mongo Shell EJSON.serialize() Example交互式Mongo Shell EJSON.serialize()示例

The following example retrieves the sales documents as an array and stores the results in the salesCollection object:以下示例以数组形式检索sales文档,并将结果存储在salesCollection对象中:

salesCollection = EJSON.serialize( db.sales.find().toArray() )

Example output, which uses JSON:示例输出,使用JSON:

[
{
_id: { '$oid': '6520519a0dbd2d208a5c7941' },
custId: 345,
purchaseDate: { '$date': '2023-07-04T00:00:00Z' },
quantity: 4,
cost: { '$numberDecimal': '100.60' }
},
{
_id: { '$oid': '6520519a0dbd2d208a5c7942' },
custId: 346,
purchaseDate: { '$date': '2023-07-12T00:00:00Z' },
quantity: 3,
cost: { '$numberDecimal': '175.45' }
},
{
_id: { '$oid': '6520519a0dbd2d208a5c7943' },
custId: 486,
purchaseDate: { '$date': '2023-08-01T00:00:00Z' },
quantity: 9,
cost: { '$numberDecimal': '200.53' }
}
]

Command Line Mongo Shell EJSON.serialize() Example命令行Mongo Shell EJSON.serialize()示例

To save collection data to a file, you can use EJSON.serialize() with the mongosh --eval method.要将集合数据保存到文件中,可以使用带有mongosh-eval方法的EJSON.serialize()

The following example retrieves the sales documents as an array and saves the results to a file named sales.json on the computer's file system:以下示例以数组形式检索sales文档,并将结果保存到计算机文件系统上名为sales.json的文件中:

# Note: The example is formatted to fit the page.

mongosh --quiet \
--eval "EJSON.serialize( db.sales.find().toArray() )" \
> sales.json

You could then use the sales.json file with an external data transformation application.然后,您可以将sales.json文件与外部数据转换应用程序一起使用。

Learn More了解更多