在 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.