Reputation: 1043
I have one collection for which I don't need any index. I just store user search term and date, so my collection is really simple.
class UserSearch {
public string Term {get; set;}
pulic DateTime Date {get;set;}
}
When I store one UserSearch item, my collection have _id and default index on it.
From my knowledge, those "_id" fields will be indexed in ram, so I don't want to spend ram memory for collection which I just store and I'm calculating something every 12 hours.
I try to delete it, but I can't.
var indexes = UserSearch(true).GetIndexes();
//delete UserSearch Default Index
if(UserSearch(true).IndexExistsByName("_id_"))
{
UserSearch(true).DropIndexByName("_id_");
}
Any suggestion/solution?
Upvotes: 11
Views: 10373
Reputation: 61225
You can't delete the _id
index once created. However you can create a collection without _id
indexes by setting the autoIndexId
option to false
using db.createCollection
but without _id
you can't replicate your collection.
Upvotes: 5
Reputation: 180917
No, you can't delete it.
From the MongoDB documentation, emphasis mine;
The _id Index
For all collections except capped collections, an index is automatically created for the _id field. This index is special and cannot be deleted. The _id index enforces uniqueness for its keys (except for some situations with sharding).
Upvotes: 19