Introduction to MongoDb with .NET part 12: deletions

Introduction

In the previous post we looked at a range of various update operations in MongoDb. We discussed the usage of a number of operators such as $unset and a set of operators used for arrays like $push and $addToSet. Finally we looked at the role of an upsert operation which is a combination of update and insert: update a document if it exists otherwise insert a new one.

In this post we’ll complete the CRUD operations in MongoDb by looking at deletions.

The remove function

A DELETE is called remove in MongoDb and the corresponding function is also called remove. It takes a filter document that specifies the documents to be removed. The filter document has the same role and JSON syntax as we saw in the case of find and update. Therefore all query techniques we’ve learnt so far can be reused here.

At this point we have a small people collection on the model database with the following records:

{ "_id" : ObjectId("57055d25b98ee40de403824d"), "name" : "john", "sport" : "football", "points" : 32, "foods" : [ "fish", "chips", "chocolate" ] }
{ "_id" : ObjectId("57055d6cb98ee40de403824f"), "name" : "john", "sport" : "baseball", "points" : 32, "foods" : [ "fish", "chips", "chocolate" ] }
{ "_id" : ObjectId("57055d9bb98ee40de4038250"), "name" : "mary", "sport" : "football", "points" : 5, "foods" : [ "fish", "chips", "chocolate" ] }
{ "_id" : ObjectId("57055daeb98ee40de4038251"), "name" : "susan", "sport" : "racketball", "points" : 22, "foods" : [ "fish", "lamb", "lamb", "chocolate" ] }
{ "_id" : ObjectId("57055dcab98ee40de4038252"), "name" : "abdul", "sport" : "cricket", "points" : 13, "foods" : [ "fish", "chips", "rice", "chocolate" ] }
{ "_id" : ObjectId("57055e0fb98ee40de4038253"), "name" : "kelly", "sport" : "tennis", "points" : 5, "foods" : [ "chips", "falafel", "chocolate", [ "pasta", "pizza", "falafel", "chips" ] ] }
{ "_id" : ObjectId("570ff0b4ce1104160e789021"), "name" : "andrew", "sport" : "swimming" }

Let’s remove the incomplete Andrew document:

db.people.remove({"name" : "andrew"})

The only document where the name property is equal to “andrew” was removed:

WriteResult({ "nRemoved" : 1 })

Let’s see what happens if we don’t supply any filter document:

db.people.remove()

We get a resounding NOPE:

2016-04-18T20:56:10.599+0200 E QUERY [thread1] Error: remove needs a query :
DBCollection.prototype._parseRemove@src/mongo/shell/collection.js:333:1
DBCollection.prototype.remove@src/mongo/shell/collection.js:356:18
@(shell):1:1

So the first query document is compulsory.

If we want to remove all documents one by one from a collection then we can supply an empty query document like we saw in the case of the update function:

db.people.remove({})

This is not a very efficient operation for large collections. However, it will leave the metadata such as the indexes of the collection untouched.

If you need to remove all documents from a collection in an efficient manner then the drop() function can help:

db.people.drop()

However it also wipes all other information about the collection.

Now let’s try to remove everyone whose foods array includes “chips”:

db.people.remove({"foods" : "chips"})

It removed 5 documents out of 6. In other words the remove function removes all documents that match the query document. This is in contrast to the update function which only updates the first matching document by default.

Note that removing many documents is not an atomic operation. This means that a document can return by the find function even if it is about to be removed as part of a remove operation.

We’re done with the CRUD operations in the MongoDb shell. In the next post we’ll start looking at the MongoDb .NET driver.

You can view all posts related to data storage on this blog here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: