made a mistake: document collection is an embedded function - mongodb

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.

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.

Mongodb: How to drop a collection named "stats"?

I created a collection named "stats" but whenever I try to delete it by using db.stats.drop() cmd line. It shows an error.
TypeError: db.stats.drop is not a function
Even tried this.
db["stats"].drop
Any solution?
if the collection exists db.stats.drop() should work (depending on the mongodb version maybe) or else perhaps db.getCollection("stats").drop();
but if all else fails, run: db.runCommand({drop : "stats"}) this should drop it if it exists without any error at least.

Is there a findById shortcut for the MongoDB shell?

The most common thing I do in the mongo DB shell is find objects by ID, eg:
db.collection.find({_id: ObjectId("55a3e051dc75954f0f37c2f2"})
I do this over and over and I find having to wrap the id with ObjectId over and over gets old. I wish I had a findById-like shorthand form like what mongoose provides. I feel like the shell ought to be smart enough to figure out what I mean here for example:
db.collection.find("55a3e051dc75954f0f37c2f2")
How might I do this? Or are there any alternative ways of querying by id in the mongo shell?
Fortunately, you can extend the shell quite easily, for example by adding the following method to the ~/.mongorc.js file which is executed when you start the mongo client:
DBCollection.prototype.findById = function(id) {
return db.getCollection(this._shortName).find( { "_id" : ObjectId(id) } );
}
Then you can execute something like db.collection.findById("55a3e051dc75954f0f37c2f2")
The shorthand for find({_id: ObjectId("...")}) is find(ObjectId("...")).

How to delete document from MongoDB using Mongoengine?

How to delete document from MongoDB using Mongoengine? I'veread the API reference here:
http://docs.mongoengine.org/apireference.html
but I can not understand what is:
delete(**write_concern)
Do you have any idea?
You can either delete an single Document instance by calling its delete method:
lunch = Food.objects.first() // Get a single 'Food' instance
lunch.delete() // Delete it!
Or you can delete all items matching a query like so:
Food.objects(type="snacks").delete()
U can use the mongoshell and issue the following command:
db.collection.remove({your condition on documents you want to remove})
for example:
From food collection you want to remove all the food which has type snacks. then you can issue the following command:
db.food.remove( { type : "snacks" } )

In Mongodb, how do I add a field to every record?

I want to add the field "bio" to "about" section of the document.
I suppose that you're using mongo console.
To add a field to each document in a collection you have to use this command:
db.foo.update({},{$set : { "about.bio" : ""}} , true, true);
Of course you have to replace foo with your real collection name. This command use empty string ("") as default value for the new field: you can change this behaviour changing the value in the command.
Hope this helps
Adding a field to each document in a collection in cmd:
cd C:\Program Files\MongoDB\Server\4.2\bin
mongo
use yourdb
db.yourcollection.update({}, {$set:{"new_field":"value"}}, false, true)
This example is made for MongoDB 4.2