在 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"
}

更新于:2019-07-30

927 次浏览

开启你的 职业

通过完成课程来获得认证

开始
广告
© . All rights reserved.