How to delete document from MongoDB using Mongoengine? - mongodb

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

Related

MongoDB update deep nested field in the document

I try to update a field in the MongoDb like this
https://mongoplayground.net/p/_jFYJGi8z46
However i get this error
fail to run update: write exception: write errors: [The array filter for identifier 'e2' was not used in the update { $set: { sizes.$[e1].wares$[e2].reserved: "2" } }]
I can't find a bug :( ... it must be a very simple one
Right... one dot was missing :( This is solved
https://mongoplayground.net/p/obdFTr6G1kj

mongoDB updateMany fields on Array

i want to update all fields on adress , i tried $ operator and alot of other stuff that on docs , but it always fail . whats am i missing?
code :
params that function gets is
To update a position of an array you should use Dot notation.
In this case address.0 addrees.1 and so on
This is an example of the data that you provided:
https://mongoplayground.net/p/kSYLDs3jPkP
I used update but you should be able to use updateMany too

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.

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

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.