Docs Home / mongosh / Reference / EJSON

EJSON.deserialize()

The EJSON.deserialize() method converts Extended JSON objects to BSON objects.EJSON.deserialize()方法将扩展JSON对象转换为BSON对象。

Syntax语法

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

EJSON.deserialize( object, [ options ] )

Method Fields方法字段

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

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

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

Boolean Value布尔值Description描述
trueReturn plain JavaScript object types. Default is true.返回纯JavaScript对象类型。默认值为true
falseReturn BSON object types.返回BSON对象类型。

Behavior行为

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

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

EJSON.deserialize( object, [ options ] )

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

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

Example示例

Create the sales collection:创建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") }
] )

The following sections show how to create an example file and then import the file with an EJSON.deserialize() example.以下部分展示了如何创建一个示例文件,然后使用EJSON.deserialize()示例导入该文件。

Create the Example File创建示例文件

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

let salesCollection = EJSON.stringify( db.sales.find().toArray() )

fs.writeFileSync( 'salesDeserialize.json', salesCollection )

Import the Example File from the Command Line从命令行导入示例文件

To import the salesDeserialize.json file and create a new collection named salesFromDeserializeFile, exit mongosh and then run this example from the command line:要导入salesDeserialize.json文件并创建名为salesFromDeserializeFile的新集合,请退出mongosh,然后从命令行运行以下示例:

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

mongosh --quiet --eval "db.salesFromDeserializeFile.insertMany( \
EJSON.deserialize( JSON.parse ( \
fs.readFileSync( 'salesDeserialize.json', 'utf8' ) ) ) )"

In the example:在示例中:

  • fs.readFileSync() reads the salesDeserialize.json file and interprets the content as utf8 Unicode character strings.读取salesDeserialize.json文件,并将内容解释为utf8 Unicode字符串。
  • JSON.parse() converts the strings read from the file to JSON.将从文件读取的字符串转换为JSON。
  • EJSON.deserialize() outputs JSON.输出JSON。
  • db.salesFromDeserializeFile.insertMany() creates and populates the salesFromDeserializeFile collection using the JSON returned by EJSON.deserialize().使用EJSON.deserialize()返回的JSON创建并填充salesFromDeserializeFile集合。

Note

You could use EJSON.parse() in the previous example, which is a simpler approach. JSON.parse() and EJSON.deserialize() provide more flexibility.您可以在前面的示例中使用EJSONparse(),这是一种更简单的方法。JSON.parse()EJSON.deserialize()提供了更大的灵活性。

Learn More了解更多