Database Reference
In-Depth Information
To remove all of the documents within the topic collection, we can execute the remove( )
statement without any parameters:
db.book.remove( )
To remove a particular book, we can execute this statement:
db.book.remove( { titleName : “Fifty Shades of Data Modeling” } )
Note that if we had five topics with the same title, the above statement will remove all five
of them.
Recall earlier that we added the topic Extreme Scoping twice in our collection:
{ _id : ObjectId(“530f8be4d77a08823086017e”), titleName : “Extreme Scoping” }
{ _id : ObjectId(“530f8be4d77a08823086017g”), titleName : “Extreme Scoping”, “subtitleName” : “An Agile Approach
to Enterprise Data Warehousing and Business Intelligence”, pageCount : 300, categories : [ “Agile”, “Business Intelli-
gence” ] }
We can now remove the duplicate:
db.book.remove( { titleName : “Extreme Scoping” } )
But don't hit the enter button just yet! This will remove both occurrences of Extreme Scop-
ing , and we only want to remove the first one. Executing the following statement would
remove just the first one:
db.book.remove( { _id : ObjectId(“530f8be4d77a08823086017e”) } )
Instead of running the above statement, we could also just remove the first occurrence of
Extreme Scoping by running this statement:
db.book.remove( { titleName : “Extreme Scoping” }, 1 )
Using 1 or true as the second parameter in the remove statement deletes only the first oc-
currence. Using 0 or false or leaving this second parameter off completely deletes all of the
documents that meet the specified criteria.
EXERCISE 4: M ONGO DB F UNCTIONALITY
Search WWH ::




Custom Search