Difference between revisions of "MongoDB Delete"
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | [[Main Page]] >> [[MongoDB|MongoDB]] >>[[MongoDB_Workbook|MongoDB Workbook]] >> Deleting a Document | + | [[Main Page]] >> [[MongoDB|MongoDB]] >>[[MongoDB_Workbook|MongoDB Workbook]] >> Deleting a Document/Collection |
== Deleting a Document == | == Deleting a Document == | ||
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.collectionName.remove(query_criteria) /* deletes all documents found that matches the query criteria */ | ||
</pre> | </pre> | ||
Line 16: | Line 16: | ||
db.collectionName.remove(query_criteria) /* deletes all documents in a collection (use with care!) */ | db.collectionName.remove(query_criteria) /* deletes all documents in a collection (use with care!) */ | ||
</pre> | </pre> | ||
+ | |||
+ | == Deleting a Collection == | ||
+ | |||
+ | If you want to drop a collection completely, including any data held in it: | ||
<pre style="color: red"> | <pre style="color: red"> | ||
− | db.collectionName.drop() /* removes a collection (use with care!) */ | + | db.collectionName.drop() /* removes a collection completely (use with care!) */ |
+ | </pre> | ||
+ | |||
+ | |||
+ | This is equivalent to the SQL command: | ||
+ | |||
+ | <pre style="color: purple"> | ||
+ | DROP TABLE tableName; | ||
</pre> | </pre> | ||
== Delete Department 40 == | == Delete Department 40 == | ||
− | Department 40 has closed down: | + | Department 40 has closed down completely and should be removed. Note, if you added the duplicate department 40 previously you will have 2 documents: |
− | db. | + | db.dept.find({deptno:40}) |
+ | |||
+ | Which should return: | ||
+ | |||
+ | <pre style="color: blue"> | ||
+ | { "_id" : 40, "deptno" : 40, "dname" : "OPERATIONS", "loc" : "BOSTON" } | ||
+ | { "_id" : 50, "deptno" : 40, "dname" : "OPERATIONS V2", "loc" : "BOSTON" } | ||
+ | </pre> | ||
+ | |||
+ | If you use the ''deleteOne()'' function, it will likely delete the first record. To delete both documents ''remove()'' must be used instead: | ||
+ | |||
+ | db.dept.remove({deptno: 40}) | ||
Note, if the query_criteria only returns one document, '''remove()''' and '''deleteOne()''' will have the same effect. | Note, if the query_criteria only returns one document, '''remove()''' and '''deleteOne()''' will have the same effect. | ||
+ | This is equivalent to the SQL command: | ||
+ | |||
+ | <pre style="color: purple"> | ||
+ | DELETE FROM dept WHERE deptno = 40; | ||
+ | </pre> | ||
Check that the collection has gone: | Check that the collection has gone: | ||
− | db. | + | 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]]. |
Latest revision as of 20:35, 13 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 completely and should be removed. Note, if you added the duplicate department 40 previously you will have 2 documents:
db.dept.find({deptno:40})
Which should return:
{ "_id" : 40, "deptno" : 40, "dname" : "OPERATIONS", "loc" : "BOSTON" } { "_id" : 50, "deptno" : 40, "dname" : "OPERATIONS V2", "loc" : "BOSTON" }
If you use the deleteOne() function, it will likely delete the first record. To delete both documents remove() must be used instead:
db.dept.remove({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.