[mongodb-csharp] How to delete all the documents in the collection?

6,861 views
Skip to first unread message

AzamSharp

unread,
May 10, 2010, 5:06:28 PM5/10/10
to mongodb-csharp
How can I delete all the documents in a particular collection? I am
having a hard time getting my head around the MongoDb C# driver.

I know I can use the collection.Delete(spec) method but what should I
pass for spec.

spec["$ne"] = ??

I want to delete all the documents whose _id is not null. How can I do
that?

Seth Edwards

unread,
May 10, 2010, 6:49:32 PM5/10/10
to mongodb...@googlegroups.com
Why not just remove the collection?

craiggwilson

unread,
May 10, 2010, 7:00:37 PM5/10/10
to mongodb-csharp
You can do Delete(new Document());

AzamSharp

unread,
May 10, 2010, 7:55:48 PM5/10/10
to mongodb-csharp
Hi Seth,

The reason I am not deleting the collection is that the collection has
indexes on it and if I delete the collection all indexes will also be
deleted.

Thanks,
Azam

AzamSharp

unread,
May 10, 2010, 7:57:01 PM5/10/10
to mongodb-csharp
Thanks I will try that out!

But how can I use my concept. I tried the following and it seems to
work.

var spec["_id"] = new Document() {{"$ne","null"}};

Thanks,
Azam

Seth Edwards

unread,
May 10, 2010, 7:57:16 PM5/10/10
to mongodb...@googlegroups.com
Gotcha, Craig's solution should do the trick.

craiggwilson

unread,
May 10, 2010, 8:26:49 PM5/10/10
to mongodb-csharp
It is only possible to have a single document in a collection with an
_id of null. So, unless there is something special about this
document, don't worry about it.

AzamSharp

unread,
May 10, 2010, 9:22:54 PM5/10/10
to mongodb-csharp
Thanks!

I am not sure what I am doing wrong but I want to remove all the
documents from a given collection at the end of each test. I am using
the following code in my TearDown method but it does not seems to be
removing the documents from the collection.

public static void RemoveAllDocumentsFromAllCollections()
{
// should not drop the complete database or else the
indexes will be gone.
// remove all records from all the collections

var mongo = new Mongo();
mongo.Connect();
var db = mongo.getDB(GetDatabaseName());

var collectionNames = db.GetCollectionNames();

foreach (var collectionName in collectionNames)
{
var collection = db.GetCollection(collectionName);
// remove every document from collection
collection.Delete(new Document());
}

mongo.Disconnect();
mongo.Dispose();
}

Thanks,
Azam

AzamSharp

unread,
May 10, 2010, 9:30:51 PM5/10/10
to mongodb-csharp
Hi,

I figured out the problem. It was the collectionName. The retrieve
collectionName was DatabaseName.CollectionName. So, I had to tweek my
code to only use the collectionName and not the Database part. Here is
the tweeked version.

public static void RemoveAllDocumentsFromAllCollections()
{
// should not drop the complete database or else the
indexes will be gone.
// remove all records from all the collections

var mongo = new Mongo();
mongo.Connect();
var db = mongo.getDB(GetDatabaseName());

var collectionNames = db.GetCollectionNames();

//db.GetCollection("users").Delete(new Document());

foreach (var collectionName in collectionNames)
{
var collectionNameAndNameSpace =
collectionName.Split('.');

if (collectionNameAndNameSpace.Length == 2)
{
var collection =
db.GetCollection(collectionNameAndNameSpace[1]);
collection.Delete(new Document());
}
}

mongo.Disconnect();
mongo.Dispose();
}

On May 10, 6:26 pm, craiggwilson <craiggwil...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages