在MongoDB中插入记录时转义引号?

双引号具有unicode,其值为\ u0022。首先让我们创建一个包含文档的集合-

> db.escapingQuotesDemo.insert({ "StudentFullName": "John \u0022 Smith" });
WriteResult({ "nInserted" : 1 })
> db.escapingQuotesDemo.insert({ "StudentFullName": "David \u0022 Miller" });
WriteResult({ "nInserted" : 1 })
> db.escapingQuotesDemo.insert({ "StudentFullName": "John \u0022 Doe" });
WriteResult({ "nInserted" : 1 })
> db.escapingQuotesDemo.insert({ "StudentFullName": "Carol \u0022 Taylor" });
WriteResult({ "nInserted" : 1 })

以下是在find()方法的帮助下显示集合中所有文档的查询-

> db.escapingQuotesDemo.find().pretty();

这将产生以下输出-

{
   "_id" : ObjectId("5ccf42e2dceb9a92e6aa195b"),
   "StudentFullName" : "John \" Smith"
}
{
   "_id" : ObjectId("5ccf42f0dceb9a92e6aa195c"),
   "StudentFullName" : "David \" Miller"
}
{
   "_id" : ObjectId("5ccf42f9dceb9a92e6aa195d"),
   "StudentFullName" : "John \" Doe"
}
{
   "_id" : ObjectId("5ccf4303dceb9a92e6aa195e"),
   "StudentFullName" : "Carol \" Taylor"
}