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

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.

Related

Delete an order from Mongo instance running on AWS

There is an Mongo DB instance running on AWS so it is can be accessed only from terminal (I guess).
the table is called orders
to see what entries are in the table I'm using this command:
db.orders.find()
it returns a list of all orders. The problem comes when I want to delete one order. If entering in terminal db.orders. and than hit the tab it returns the list of all available commands.
One that I need I think it's db.orders.deleteOne() but I don't know what argument to send to it.
The order that must be deleted looks like this:
{ "_id" : ObjectId("5ea1a1c48a0c055870168770"), "display_id" : "WOW" }
so I tried with in many ways like:
db.orders.deleteOne(5ea1a1c48a0c055870168770)
db.orders.deleteOne("5ea1a1c48a0c055870168770")
db.orders.deleteOne(_id: 5ea1a1c48a0c055870168770)
db.orders.deleteOne("_id": "5ea1a1c48a0c055870168770")
but none of the seem to work.
db.orders.deleteOne( { "_id" : ObjectId("563237a41a4d68582c2509da") } );

Mongodb copy a collection into an other collection

I need to copy a collection into an other collection to work with. What I have so far is
checkPoints.find().forEach(function(copy){'pdiCheckPoints'[MachineNr].insert(copy);});
But mongo throws the TypeError: Cannot call method 'insert' of undefined.
The copy works with
checkPoints.find().forEach(function(copy){pdiCheckPoints.insert(copy);});
but working with more than 1 Machine i need to add an id for each copy somehow.
Any help is highly appreciated.
You can simply augment your document with an additional key during the copy:
checkPoints.find().forEach(function(doc){
doc.machineNumber = MachineNr;
pdiCheckPoints.insert(doc);
});
i think this is help to u
db.oldCollection.aggregate([{$out : "newCollection"}])

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" } )

Can not delete collection from mongodb

Can not delete the collection from the shell,
The thing that the collection is available and my php script is accessing it (selecting|updating)
But when I used:
db._registration.drop()
it gives me an error:
Date, JS Error: TypeErrorL db._registration has no properties (shell): 1
The problem is not with deleting the collection. The problem is with accessing the collection. So you would not be able to update, find or do anything with it from the shell. As it was pointed in mongodb JIRA, this is a bug when a collection has characters like _, - or .
Nevertheless this type of names for collections is acceptable, but it cause a problem in shell.
You can delete it in shell with this command:
db.getCollection("_registration").drop()
or this
db['my-collection'].drop()
but I would rather rename it (of course if it is possible and will not end up with a lot of changing).
You can also use:
db["_registration"].drop()
which syntax works in JS as well.
For some reason the double quotes "_registration" did not workfor me .. but single quote '_registration' worked

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.