Difference between revisions of "MongoDB Delete"
From mi-linux
Jump to navigationJump to searchLine 42: | Line 42: | ||
This is equivalent to the SQL command: | This is equivalent to the SQL command: | ||
+ | <pre style="color: blue"> | ||
DELETE FROM dept WHERE deptno = 40; | DELETE FROM dept WHERE deptno = 40; | ||
</pre> | </pre> | ||
Line 48: | Line 49: | ||
db.dept.find({deptno:40}) | db.dept.find({deptno:40}) | ||
+ | |||
+ | No documents should be returned. | ||
== Next Step == | == Next Step == | ||
This concludes the section on manipulating collections. Return to the [[MongoDB_Workbook|Workbook]]. | This concludes the section on manipulating collections. Return to the [[MongoDB_Workbook|Workbook]]. |
Revision as of 17:04, 12 November 2017
Main Page >> MongoDB >>MongoDB Workbook >> Deleting a Document/Collection
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(query_criteria) /* 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!) */
This is equivalent to the SQL command:
DROP TABLE tableName;
Delete Department 40
Department 40 has closed down:
db.dept.deleteOne({deptno:40})
Note, if the query_criteria only returns one document, remove() and deleteOne() will have the same effect.
This is equivalent to the SQL command:
DELETE FROM dept WHERE deptno = 40;
Check that the collection has gone:
db.dept.find({deptno:40})
No documents should be returned.
Next Step
This concludes the section on manipulating collections. Return to the Workbook.