Escaping quotes while inserting records in MongoDB?


The double quotes have unicode which has the value \u0022. Let us first create a collection with documents −

> 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 })

Following is the query to display all documents from a collection with the help of find() method −

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

This will produce the following output −

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

Updated on: 30-Jul-2019

763 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements