Can I remove the default MongoDB index?

I have one collection for which I don't need any index. I just keep the user term and date, so my collection is very simple.

class UserSearch { public string Term {get; set;} pulic DateTime Date {get;set;} } 

When I store one UserSearch element, my collection has _id and a default index.

According to my information, these "_id" fields will be indexed in ram, so I don’t want to waste RAM memory for the collection that I just store, and I calculate something every 12 hours.

I am trying to remove 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?

+6
source share
2 answers

No, you cannot delete it.

From the MongoDB documentation , my emphasis is;

Index _id

For all collections except private collections, an index is automatically created for the _id field. This index is special and cannot be removed . The _id index provides uniqueness for its keys (with the exception of some fragments).

+12
source

You cannot remove the _id index after creation. However, you can create a collection without _id indices by setting the autoIndexId option to false using db.createCollection but without _id you cannot replicate your collection.

+1
source

Source: https://habr.com/ru/post/925473/


All Articles