Mongodb command 'show collections' shows nonexistent collections - mongodb

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.

Related

mongo: collection not found ReferenceError: auditoria is not defined

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.

made a mistake: document collection is an embedded function

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.

Meteor/MongoDB limiting the result

I am trying to find all documents and publish at most 5 from the results.
Following this section of the MongoDB doc, I am trying to do this:
Meteor.publish('teams', function () {
return Teams.find().limit(5);
});
Yet, in the server console, I get an exception:
Exception from sub teams id Pm6jKL8Sv3FSDSTfM TypeError: Object [object Object] has no method 'limit'
The following works fine:
Meteor.publish('teams', function () {
return Teams.find({}, {limit:5});
});
Why does the second way work, rather than the first? And where can I find documentation for it?
Meteor's collection API is somewhat different from that of the mongo API. find takes up to two parameters: a selector object, and an options object. options allows you to specify such things as sort, skip, limit and fields, in addition to the meteor-specific reactive and transform.

Unable to access MongoDB collection from shell when the name contains '-'

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.

MongoDB Shell - access collection with period in name?

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.