I'm getting this message on a very simple find command:
rs0:PRIMARY> use MPI
switched to db MPI
rs0:PRIMARY> show collections;
hes-auditoria-mpi-fhir
hes-auditoria-schemas
rs0:PRIMARY> db.hes-auditoria-mpi-fhir.find();
2021-05-20T14:20:05.502+0200 E QUERY [js] ReferenceError: auditoria is not defined :
#(shell):1:1
any ideas?
Use getCollection helper:
db.getCollection("hes-auditoria-mpi-fhir").find()
It's been added exactly for this purpose. From the docs:
db.getCollection(name)
Returns a collection or a view object that is functionally equivalent to using the db.<collectionName> syntax. The method is useful for a collection or a view whose name might interact with the mongo shell itself, such as names that begin with _ or that match a database shell method.
Related
so, I managed to create a document collection called version ...
starting up the mongo shell, I have this
> show collections
customer
system.indexes
version
db.customer.find() gives me a list of my customer data
db.version.find() gives a syntax error
2015-11-19T22:12:59.598+0000 TypeError: Object function (){
return this.serverBuildInfo().version;
} has no method 'find'
and db.version() gives
> db.version()
2.6.6
so I am assuming that version is a function in mongo.
To come to the question I need to ask :
how can I get the data of this collection from the mongo shell ? Or can I rename the collection ?
thanks !
You can access that collection via:
> db.getCollection('version')
and rename it via:
> db.getCollection('version').renameCollection('mynewname')
Haven't tried this in your scenario, but you can try using the Mongo admin command renameCollection.
use admin
db.runCommand( { renameCollection: "test.version", to: "test.foo" } )
Then once the collection is off the reserved name you can do what you want with the new colection.
In mongo shell:
> show collections
25420768
37310514
38222868
39065677
40516351
40583840
40892914
41005003
42991119
Let's look at the 25420768 collection:
> db['25420768'].find({})
2015-08-28T16:15:23.568-0500 TypeError: Cannot call method 'find' of undefined
> db['25420768'].drop()
2015-08-28T16:16:58.577-0500 TypeError: Cannot call method 'drop' of undefined
How do I remove these phantom (and admittedly poorly named) collections from my database?
Just had the same error,
db['25420768'].drop() wouldn't work, but db.getCollection('25420768').drop() would.
I want to make my mongoDB more secure. I read, that I can start it with --noscripting to deny JavaScript. I tried to verify, that this is working.
When I started the DB like this
mongod --dbpath /var/lib/mongodb --noscripting --fork --logpath /var/log/mongodb/mongod.log
and then I tried to use the $where-Operator like it is described here:
http://docs.mongodb.org/manual/reference/operator/query/where/
for example:
db.myCollection.find({$where: "_id == 1"})
it is returning:
Error: error: {
"$err" : "Can't canonicalize query: BadValue no globalScriptEngine in $where parsing",
"code" : 17287
}
Is this the answer, I should expect? Or is it pointing to another failure?
This is expected behavior since the --noscripting option disables the scripting engine and all the $where operator does is JavaScript evaluation as mention in the official documentation;
Use the $where operator to pass either a string containing a JavaScript expression or a full JavaScript function to the query system. The $where provides greater flexibility, but requires that the database processes the JavaScript expression or function for each document in the collection. Reference the document in the JavaScript expression or function using either this or obj .
Also the error message is pretty clear.
..."$err" : "Can't canonicalize query: BadValue no globalScriptEngine in $where parsing"
Note the no globalScriptEngine part of the message.
I have a collection named GoldenGlobes-emotion in my MongoDB 2.6.9
I found I can not access this collection from the MongoDB shell
When ever I try to access the collection, for example
db.GoldenGlobes-emotion.findOne()
I always got this:
ReferenceError: emotion is not defined
But it works well when I access the collection form Python with PyMongo.
Is this a shell bug?
Or '-' is a reserved character?
Try db["GoldenGlobes-emotion"].findOne().
The MongoDB shell is a Javascript interpreter. Javascript does not allow hyphens in variable names, because it interprets them as the minus-operator. However, you can also access object-fields with string literals by using the array-syntax. In that case, this restriction does not apply.
I have found a collection in one of our MongoDB databases with the name my.collection.
Is there a way to access this collection from the MongoDB shell, despite it having a point in the name?
> db.my.collection.findOne();
null
I'm pretty sure that that is not correct.
try this instead:
db["my.collection"].findOne();
you run into the same issue with hyphens or any other name that does not match on
[a-zA-Z_$][0-9a-zA-Z_$]
This limitation comes from valid named for javascript object properties.
if collection name is "my.collection"
db.my.collection.findOne(); // OK
null
if collection name is "my.1.collection"
db.my.1.collection.findOne(); // Not OK
SyntaxError: missing ; before statement
Fix:
db["my.1.collection"].findOne(); // Now is OK
null
Another foolproof method is:
db.getCollection("_SCHEMA").find()
While in the case of a underscore in the name, still cause a error with #Laura answer:
> db["_SCHEMA"].find()
2016-07-18T17:44:16.948+0200 E QUERY [thread1] TypeError: db._SCHEMA is undefined :
#(shell):1:1
Your code is correct. If it returns null it means that the collection is empty.