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!
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 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.
the following code leaves an empty dummy database. Is this system behavior intended?
mongodb is running --auth mode and the user is part of the readAnyDatabase Role.
import pymongo
print CORE_PROD_URL
mongo = pymongo.MongoClient(CORE_PROD_URL)
print mongo.database_names()
print mongo.dummy.test.count()
print mongo.database_names()
which gives:
mongodb://read_only_user:pw#localhost:27017
[u'admin', u'local']
0
[u'admin', u'local', u'dummy']
the same behaviour happens with find()
while
mongo.dummy.test.insert({‘foo’: ‘bar’})
throws an exception
OperationFailure: not authorized on new_db to execute command
This is a known bug, SERVER-11051. The database name will disappear from "database_names()" the next time you restart the server, but of course it will reappear next time you read from the "dummy" database.
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])
I am running Debian with MongoDB shell version: 2.4.3
I run
use dbname
db.stats.find()
And it outputs the following
> db.stats.find()
Mon May 13 17:55:20.933 JavaScript execution failed: TypeError: Object function (scale){
return this.runCommand( { dbstats : 1 , scale : scale } );
} has no method 'find'
However running it on other collections works fine.
This mongo instance is being used with nodejs.
If you really created a collection named stats in your database dbname then I would advise you to rename it. In the shell the db object has a stats() method for looking at statistics of the database.
Meanwhile you can use slightly more complex syntax:
> db.getSiblingDB("dbname").getCollection("stats").find()
Fetched 0 record(s) in 4ms
Or if you are in dbname then:
> db.getCollection("stats").find()
I presume you want db.stats().