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 MongoDB使用BSON存储数据。许多外部数据转换应用程序使用JSON。您可以使用EJSON.serialize() to convert BSON to JSON and save the output for those external applications.EJSON.serialize()将BSON转换为JSON,并为这些外部应用程序保存输出。
Syntax语法
The method has this syntax:该方法具有以下语法:
EJSON.serialize( object, [ options ] )Method Fields方法字段
The method takes the following fields:该方法接受以下字段:
object | BSON object | ||||||||
options | string |
|
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了解更多
- EJSON serialize method
- EJSON documentation