Docs Home / mongosh / Reference / EJSON

EJSON.parse()

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()方法接受以下字段:

Field字段Type类型Necessity必要性Description描述
valuestringRequired必需String EJSON.parse() transforms into JSON key-value pairs字符串EJSON.parse()转换为JSON键值对
optionsstringOptional可选

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

ValueEffect效果
trueReturns native JavaScript types when possible尽可能返回本机JavaScript类型
falsePrefer to return BSON types更倾向于返回BSON类型

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 salesCollection as JSON pairs.salesCollection中的值格式化为JSON对。
  • db.salesRestored.insertMany() uses the JSON pairs to create the salesRestored collection.使用JSON对创建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 EJSON.parse() with the mongosh --eval method.要从外部源(如文件或API调用)导入字符串数据,请使用带有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 the sale.json file as a string.将字符串作为输入。此示例使用fs.readFileSync()sale.json文件读取为字符串。
  • EJSON.parse() formats the input string as JSON pairs.将输入字符串格式化为JSON对。
  • db.salesFromFile.insertMany() creates the salesFromFile collection from the JSON pairs.从JSON对创建salesFromFile集合。

Learn More了解更多