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:该方法接受以下字段:
object | EJSON | ||||||||
options | string |
|
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.jsonfile and interprets the content asutf8Unicode character strings.salesDeserialize.json文件,并将内容解释为utf8Unicode字符串。JSON.parse()converts the strings read from the file to JSON.将从文件读取的字符串转换为JSON。EJSON.deserialize()outputs JSON.输出JSON。db.salesFromDeserializeFile.insertMany()creates and populates the使用salesFromDeserializeFilecollection using the JSON returned byEJSON.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了解更多
EJSON deserialize methodEJSON反序列化方法- EJSON
documentation文档