When I am inserting data in MongoDb database using cmd command
mongo mongodb://localhost:27017/DummyDatabase --eval "db.Dummy.insert({a:12345,b:asd})"
I am getting following error -
MongoDB shell version v4.2.0
connecting to: mongodb://localhost:27017/Ontologies?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6da84ed8-8bf0-4a6c-9d38-7c70cbfc8c7e") }
MongoDB server version: 4.2.0
2019-11-07T13:49:15.316+0530 E QUERY [js] uncaught exception: ReferenceError: asd is not defined :
#(shell eval):1:38
2019-11-07T13:49:15.320+0530 E - [main] exiting with code -4
But when I give this command -
mongo mongodb://localhost:27017/DummyDatabase --eval "db.Dummy.insert({a:12345})"
This works, what can be the issue?
The issue with failing command is that the value provided to the field with key 'b' is not a legal JSON value type. asd is a string that needs to be enclosed in quotes so that the document being inserted can be a valid JSON object. Refer to json.org for details.
Related
how to prevent the mongo db connection messages while exporting query results to the csv?
used --quiet command but its not working when I connect to remote DB?
C:\test>mongo test.js > test.csv
MongoDB shell version v5.0.6
connecting to: mongodb://xxx.x.xx.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5858-78a6-4e23-83d3-320e7883fd7b") }
MongoDB server version: 5.0.6
When I run the following script:
mongo -u root myinitscript.js
And myinitscript.js has the following code:
use somedbname
db.createUser({
user: 'someuser',
pwd: 'somepass',
roles: [{
role: 'dbOwner',
db: 'somedbname'
}]
});
Produces the following error:
MongoDB shell version v4.4.0
Enter password:
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("352e2ff4-3c4b-4e4c-b898-b67450c13627") }
MongoDB server version: 4.4.0
uncaught exception: SyntaxError: unexpected token: identifier :
#mongodb_init.js:1:4
failed to load: mongodb_init.js
exiting with code -3
When I remove use somedbname from the script - it works, but does NOT create the database, which is my primary goal. Running use somedbname under mongo shell works just fine, but is NOT an automation.
Any hints on how to solve this?
use dbname is a special shell helper. When you load a js file you can't use this helper because its syntax is not valid js.
Use getSiblingDb instead in js files.
You can also use the connection string (mongodb:// URI) as the shell argument and include the database in the connection string.
when I run any query containing $expr operation against Embedded Mongo I get the following error:
UncategorizedMongoDbException: Query failed with error code 2 and error message 'unknown top level operator: $expr' on server
The command runs fine against my local instance of mongo.
This is the version of embedded mongo I'm using: testCompile('de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.1.1')
This is the query for reference:
Criteria.where("$expr").ne(Arrays.asList("$val.a", "$val.b"))
Found it.
flapdoodle was downloading a version of Mongodb that didn't have that feature by default.
You can override the default version by specifying the following in your
src/test/resources/application.properties
spring.mongodb.embedded.version=3.6.4
spring.mongodb.embedded.features=SYNC_DELAY,NO_HTTP_INTERFACE_ARG,ONLY_WITH_SSL
I've written a script that takes in some variables and updates my db accordingly. I seem to get a connection to the wrong database every time I try to run my script in the command line:
conn = new Mongo();
db = conn.getDB("my_db");
db.auth({
user:DB_USER,
password: DB_PASSWORD
});
cust = db.customers;
print('===== Indexes before execution =====');
print(JSON.stringify(db.customers.getIndexes()));
cust.createIndex({geo_point:"2dsphere"});
print('===== Indexes after execution =====');
print(JSON.stringify(db.customers.getIndexes()));
This is how I'm running it: mongo --eval 'let DB_USER="usr", DB_PASSWORD="pwd"' script.js
and this is the output: MongoDB shell version: 3.2.16
connecting to: test
2017-09-20T22:51:08.589+0000 E QUERY [thread1] SyntaxError: missing ; before statement #(shell eval):1:4
As you can see it says "connecting to test". Not sure what that's about, also, not sure what it means by missing ; before statement #(shell eval):1:4
Please note that when I run mongo in the shell I get
MongoDB shell version: 3.2.16
connecting to: test
All help is greatly appreciated. Thanks!
i try to add new member to replicas set in mongodb. there is possibility to
do it with os.system. But how would it be with subprocess.call()?
So far i have:
import subprocess
task='''"rs.add('alehandro-VirtualBox:27067')"'''
port=27072
subprocess.call(["/usr/bin/mongo", " --port {0}".format(port), " --eval {0}".format(task)])
OUTPUT:
MongoDB shell version: 2.4.5
connecting to: --port 27072
Sun Jul 28 16:34:18.884 JavaScript execution failed: Error: [ --port 27072] is not a valid database name at src/mongo/shell/mongo.js:L40
exception: connect failed
Can anybody help me with it?
The mongo process is being invoked with a parameter called --port 27072 and a value of --eval .. because of the way you are passing your parameters to subprocess.call.
If you change the subprocess.call invocation to the following, then it should work :
subprocess.call(["/usr/bin/mongo", "--port", str(port), "--eval", task])