Difference between revisions of "MongoDB Delete"
From mi-linux
Jump to navigationJump to searchLine 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 | + | <pre style="color: blue"> |
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 | + | <pre style="color: blue"> |
db.collectionName.remove(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 */ | ||
</pre> | </pre> | ||
− | <pre style:"color: | + | <pre style:"color: red"> |
db.collectionName.remove() /* deletes all documents in a collection (use with care!) */ | db.collectionName.remove() /* deletes all documents in a collection (use with care!) */ | ||
</pre> | </pre> | ||
− | <pre style:"color: | + | <pre style:"color: red"> |
db.collectionName.drop() /* removes a collection (use with care!) */ | db.collectionName.drop() /* removes a collection (use with care!) */ | ||
</pre> | </pre> |
Revision as of 13:08, 21 October 2016
Main Page >> MongoDB >>MongoDB Workbook >> Deleting a Document
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.collectionName.remove(query_criteria) /* deletes all documents found that matches the query criteria */
db.collectionName.remove() /* deletes all documents in a collection (use with care!) */
db.collectionName.drop() /* removes a collection (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.