Insert document with string containing line breaks to mongo using mongo shell - mongodb

I am trying to insert the following document into a mongo collection:
[
{
"text": "tryng to insert a string
with some line breaks",
}
]
By running db.myCollection.insertMany(documentArray) where documentArray is just me copy-pasting this array.
But I am getting this error:
> db.myCollection.insertMany([
... {
... "text": "tryng to insert a string
uncaught exception: SyntaxError: "" literal not terminated before end of script :
#(shell):3:37
> with some line breaks",
uncaught exception: SyntaxError: missing ( before with-statement object :
#(shell):1:5
> }
uncaught exception: SyntaxError: expected expression, got '}' :
#(shell):1:0
> ]
Which obviously appears because it detects the new line character as the end of the command, so Mongo shell thinks it has to run the command, which is not complete.
Is there any way of saving \r\n and \n characters in MongoDB? Should I use another method not directly with the shell?
Both Mongo and the shell are version 4.4.15

Try doing this instead. The key is to embed the newline character in the string.
[
{
"text": "trying to insert a string\n" +
"with some line breaks",
}
]

Related

mongo shell command not accept use db command

i am trying to pull records form mongo data base with json format to do that i am running below js :
conatin of get_DRMPhysicalresources_Data.js
cursor = db.physicalresources.find().limit(10)
while ( cursor.hasNext() ){
print( JSON.stringify(cursor.next()) );
}
the command i run to get the records :
mongo host_name:port/data_base_name -u user -p 'password' --eval "load(\"get_DRMPhysicalresources_Data.js\")" > DRMPhysicalresources.json
and i am able to get the result as josn formate inside DRMPhysicalresources.json , now i want to switch to other data base using use command i try add use db as below :
conatin of get_DRMPhysicalresources_Data.js
use db2_test
cursor = db.physicalresources.find().limit(10)
while ( cursor.hasNext() ){
print( JSON.stringify(cursor.next()) );
}
the command i run to get the records :
mongo host_name:port/data_base_name -u user -p 'password' --eval "load(\"get_DRMPhysicalresources_Data.js\")" > DRMPhysicalresources.json
but i am getting below errors :
MongoDB shell version v4.2.3
connecting to: "some data base info"
Implicit session: session { "id" : UUID("8c85c6af-ebed-416d-9ab8-d6739a4230cb") }
MongoDB server version: 4.4.11
WARNING: shell and server versions do not match
2022-04-11T13:39:30.121+0300 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell eval):1:1
2022-04-11T13:39:30.122+0300 E QUERY [js] Error: error loading js file: get_DRMPhysicalresources_Data.js :
#(shell eval):1:1
2022-04-11T13:39:30.122+0300 E - [main] exiting with code -4
is there are any way to add use db2_test without break it ?
You can try this:
db = new Mongo().getDB("myOtherDatabase");
or
db = db.getSiblingDB('myOtherDatabase')
( this is the exact js equivalent to the 'use database' helper)
docs

when i try to run the updateOne method in mongoDB , i get this error uncaught exception: SyntaxError: illegal character :

The Query:
db.products.updateOne(
{"name":"pen"},
{$set : { "stock" : 32}},
)
I got this error:
uncaught exception: SyntaxError: illegal character :
Try to change $set with "$set". Also, enter the code in the question instead of putting images.

mongo db error - SyntaxError: missing ; before statement

when i try to run this command on cmd.
mongo test --eval 'db.testcollection.find({ createdate: {$gte: ISODate("2020-07-28 10:08:37.579Z"),$lt: ISODate("2020-08-04 13:42:40.975Z")}}, {place:1, _id:0}).forEach(function(x) { print(x.place); })'
it throws this error
MongoDB server version: 4.0.5
2020-08-06T13:33:49.336+0530 E QUERY [js] SyntaxError: missing ; before statement #(shell eval):1:19
i try to add a semicolon at the end. it still throws same error.
mongo test --eval 'db.testcollection.find({ createdate: {$gte: ISODate("2020-07-28 10:08:37.579Z"),$lt: ISODate("2020-08-04 13:42:40.975Z")}}, {place:1, _id:0}).forEach(function(x) { print(x.place); });'
Error is at line 1, column 19. So placing ; at the end will not solve the issue.
Could you use
mongo --eval 'db.testcollection.find({ createdate: {$gte: ISODate("2020-07-28 10:08:37.579Z"),$lt: ISODate("2020-08-04 13:42:40.975Z")}}, {place:1, _id:0}).forEach(function(x) { print(x.place); })' test
test is the db name where testcollection is present.

Syntax Error: missing ) after argument list #(shell):2:4

I am getting the above error in Mongo DB when entering the following in the shell but I can't for the life of me see where there is a syntax error...
db.createUser (
... "user":"dbTest",
... "pwd":"testPass",
... "roles": [
... { "role":"readWrite", "db":"test" }
... ]
... )
That has been copied and pasted directly from the console.
You're missing curly braces around the object literal:
db.createUser ({
"user":"dbTest",
"pwd":"testPass",
"roles": [
{ "role":"readWrite", "db":"test" }
]
})
See db.createUser() - Examples

Pass a .js file to mongo db.eval()

I am trying to get variety working with db.eval() instead of commandline --eval. This is because I get some problems with authentication.
The normal use is from the commandline:
$ mongo test --eval "var collection = 'users', maxDepth = 3" /path/to/variety.js
What I am trying to do is this:
$ mongo
>> use admin
>> db.auth("foo", "bar")
>> use anotherColl
>> db.eval("/path/to/variety.js", "var collection = 'users', maxDepth = 3")
>> Thu Mar 7 13:19:10 uncaught exception: {
"errmsg" : "compile failed: JS Error: SyntaxError: invalid flag after regular expression nofile_a:0",
"ok" : 0
}
Is there a way to have db.eval() eat an javascript file instead of an string?
According to #Sammaye and my gut feeling you can't put in .js files in db.eval(). However for variety there is an solution described here.
mongo -u USER-p PASS admin --eval "var collection = 'COL', db_name='DB_NAME'" variety.js
And now the authentication problem :(