Can i delete default MongoDB Index?

2019-04-25 17:13发布

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?

2条回答
ら.Afraid
2楼-- · 2019-04-25 17:32

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.

查看更多
等我变得足够好
3楼-- · 2019-04-25 17:55

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).

查看更多
登录 后发表回答