Difference between revisions of "MongoDB DeleteNested"
From mi-linux
Jump to navigationJump to search (Created page with "Main Page >> MongoDB >>MongoDB Workbook >> Deleting a Document/Collection with nested data == Deleting a Document == At some stage you m...") |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
At some stage you may want to delete a document from a collection. The format is: | At some stage you may want to delete a document from a collection. The format is: | ||
− | <pre style="color: | + | <pre style="color: purple"> |
db.collectionName.deleteOne(query_criteria) /* deletes the first document found that matches the query criteria */ | db.collectionName.deleteOne(query_criteria) /* deletes the first document found that matches the query criteria */ | ||
</pre> | </pre> | ||
− | <pre style="color: | + | <pre style="color: purple"> |
− | db.collectionName.remove(query_criteria) /* deletes all documents found that matches the query criteria */ | + | db.users.deleteMany(query_criteria) /* deletes all documents found that matches the query criteria */ |
+ | </pre> | ||
+ | |||
+ | <pre style="color: purple"> | ||
+ | db.collectionName.remove(query_criteria) /* deletes all documents found that matches the query criteria. Similar to deleteMany() */ | ||
</pre> | </pre> | ||
<pre style="color: #ff0000"> | <pre style="color: #ff0000"> | ||
− | db.collectionName.remove( | + | db.collectionName.remove({}) /* deletes all documents in a collection (use with care!) */ |
</pre> | </pre> | ||
Latest revision as of 11:26, 2 June 2019
Main Page >> MongoDB >>MongoDB Workbook >> Deleting a Document/Collection with nested data
Deleting a Document
At some stage you may want to delete a document from a collection. The format is:
db.collectionName.deleteOne(query_criteria) /* deletes the first document found that matches the query criteria */
db.users.deleteMany(query_criteria) /* deletes all documents found that matches the query criteria */
db.collectionName.remove(query_criteria) /* deletes all documents found that matches the query criteria. Similar to deleteMany() */
db.collectionName.remove({}) /* deletes all documents in a collection (use with care!) */
Deleting a Collection
If you want to drop a collection completely, including any data held in it:
db.collectionName.drop() /* removes a collection completely (use with care!) */
Delete Department 40
Department 40 has closed down:
db.deptCollection.deleteOne({deptno:40})
Note, if the query_criteria only returns one document, remove() and deleteOne() will have the same effect.
Check that the collection has gone:
db.deptCollection.find({deptno:40})
Next Step
This concludes the section on manipulating collections. Return to the Workbook.