The EJSON.parse() method converts string values to JSON.EJSON.parse()方法将字符串值转换为JSON。
Syntax语法
The EJSON.parse() method takes a string as input and an optional modifier that controls the output format.EJSON.parse()方法接受一个字符串作为输入,并接受一个可选的修饰符来控制输出格式。
EJSON.parse(string, [options])Command Fields命令字段
The EJSON.parse() method takes these fields:EJSON.parse()方法接受以下字段:
value | string | EJSON.parse() transforms into JSON key-value pairsEJSON.parse()转换为JSON键值对 | |||||||
options | string |
|
Behavior行为
You can call 您可以从交互式EJSON.parse() from inside an interactive mongosh session or from the system command line using --eval.mongosh会话内部或使用--eval从系统命令行调用EJSON.parse()。
Call 在交互式会话中调用EJSON.parse() from an interactive session:EJSON.parse():
EJSON.parse(string)
Call 从系统命令行调用EJSON.parse() from the system command line:EJSON.parse():
mongosh --eval "EJSON.parse(string)"Examples示例
To try these examples, first 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"), },
] )
Format Input with EJSON.parse()使用EJSON.parse()格式化输入
EJSON.parse() accepts a string as input. 接受字符串作为输入。For this example, use the EJSON.stringify() method to export the 对于这个例子,使用sales collection as a string.EJSON.stringify()方法将销售收款导出为字符串。
let salesCollection = EJSON.stringify( db.sales.find().toArray() )
Use 使用EJSON.parse() to format the string for methods like db.collection.insertMany() that expect JSON pairs:EJSON.parse()为db.collection.insertMany()等需要JSON对的方法格式化字符串:
db.salesRestored.insertMany( EJSON.parse( salesCollection ) )
EJSON.parse()formats the values in将salesCollectionas JSON pairs.salesCollection中的值格式化为JSON对。db.salesRestored.insertMany()uses the JSON pairs to create the使用JSON对创建salesRestoredcollection.salesFrestored集合。
Use EJSON.parse() from the command line从命令行使用EJSON.parse()
To import string data from an external source such as a file or an API call, use 要从外部源(如文件或API调用)导入字符串数据,请使用带有EJSON.parse() with the mongosh --eval method.mongosh --eval方法的EJSON.parse()。
For this example, save the 对于此示例,将sales collection as a file.sales集合另存为文件。
let salesCollection = EJSON.stringify( db.sales.find().toArray() )
fs.writeFileSync( 'sales.json', salesCollection )
The code creates a file on your local system called 该代码在本地系统上创建了一个名为sales.json. To import the file and create a new collection, exit mongosh and run an --eval operation from the command line.sales.json的文件。要导入文件并创建新集合,请退出mongosh并从命令行运行--eval操作。
# Note: This example is formatted to fit on the page.
mongosh --quiet \
--eval "db.salesFromFile.insertMany( \
EJSON.parse( fs.readFileSync( 'sales.json', 'utf8' ) ) )"
EJSON.parse()takes a string as input. This example uses将字符串作为输入。此示例使用fs.readFileSync()to read thesale.jsonfile as a string.fs.readFileSync()将sale.json文件读取为字符串。EJSON.parse()formats the input string as JSON pairs.将输入字符串格式化为JSON对。db.salesFromFile.insertMany()creates the从JSON对创建salesFromFilecollection from the JSON pairs.salesFromFile集合。
Learn More了解更多
- EJSON
documentation文档 Mozilla Developer Network JSON.parse documentationMozilla开发者网络JSON.parse文档