Delete by id mongodb node js
Why I can't remove record by _id? Code:
JohnnyHK 295k63 gold badges603 silver badges457 bronze badges asked Oct 15, 2012 at 18:17
You need to pass
the
answered Oct 15, 2012 at 18:39
JohnnyHKJohnnyHK 295k63 gold badges603 silver badges457 bronze badges 3 MongoDb has now marked the remove method as deprecated. It has been replaced by two separate methods: deleteOne and deleteMany. Here is their relevant getting started guide: https://docs.mongodb.org/getting-started/node/remove/ and here is a quick sample:
answered Mar 14, 2016 at 7:31
Bill TarbellBill Tarbell 4,6282 gold badges32 silver badges49 bronze badges With TypeScript, you can to it using imports, instead of requiring the whole library
answered Dec 19, 2020 at 6:21
Len JosephLen Joseph 1,2649 silver badges20 bronze badges First include mongodb
You have to include the ObjectID from mongodb
Then Use
1000% works...
answered Dec 9, 2020 at 16:45
i think we have to require mongodb as const and use it with mongodb answered Sep 7, 2021 at 10:43
I recently stumbled with this problem today and I find that the fix is:
answered Feb 3 at 15:41
1 Delete DocumentTo delete a record, or document as it is called in MongoDB, we use the The first parameter of the Note: If the query finds more than one document, only the first occurrence is deleted. ExampleDelete the document with the address "Mountain 21": var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » Save the code above in a file called "demo_delete.js" and run the file: Run "demo_delete.js" C:\Users\Your Name>node demo_delete.js Which will give you this result: Delete ManyTo delete more than one document, use the The first parameter of the ExampleDelete all documents were the address starts with the letter "O": var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » Save the code above in a file called "demo_delete_many.js" and run the file: Run "demo_delete_many.js" C:\Users\Your Name>node demo_delete_many.js Which will give you this result: The Result ObjectThe Most of the information is not important to understand, but one object inside the object is called "result" which tells us if the execution went OK, and how many documents were affected. The result object looks like this: You can use this object to return the number of deleted documents: ExampleReturn the number of deleted documents: console.log(obj.result.n); Which will produce this result: |