MongoDB - escaping quotes when inserting a record

I had a strange problem when I tried to write scritp bash to copy some data from one database to another.

To make everything simple, I will talk about this in the following example:
Let's say we have a file that contains the mongo paste commands that need to be executed in mongo client. With Bashit will be:

cat file.json | mongo --verbose --host $HOST

This works fine until we use qoutes in the contents of the entries.
For example:

use somedb;
db["collection"].insert({ "field": "test" }) 
#This of course works

db["collection"].insert({ "field": "test \" test" }) 
#But this doesn't

db["collection"].insert({ "field": "test \\" test" }) "#<-For syntax coloring
#I thounght maybe double quoting will help, but this also doesn't work

db["collection"].insert({ "field": "\"test\"" }) 
#This SUPRISINGLY works!!!

, mongo ( MongoDB shell verions: 2.2.4)? , script ? , . Mongo ( --verbose), .

+4
1

JIRA ticket , 2.5.0.

unicode :

> db.foo.insert({ "field": "test \u0022 test" })
> db.foo.find()
{ "_id" : ObjectId("533455e563083f9b26efb5c2"), "field" : "test \" test" }
+4

Source: https://habr.com/ru/post/1533772/


All Articles