Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i delete default MongoDB Index?

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?

like image 784
Novkovski Stevo Bato Avatar asked Sep 15 '12 16:09

Novkovski Stevo Bato


People also ask

How do I delete an index in MongoDB?

Starting in MongoDB 5.2, you can use db. collection. dropIndex() to drop existing indexes on the same collection even if there is a build in progress on another index.

Do we need to rebuild index in MongoDB?

According to MongoDB documentation: Normally, MongoDB compacts indexes during routine updates. For most users, the reIndex command is unnecessary. However, it may be worth running if the collection size has changed significantly or if the indexes are consuming a disproportionate amount of disk space.

Does order of index matter MongoDB?

Indexes store references to fields in either ascending ( 1 ) or descending ( -1 ) sort order. For single-field indexes, the sort order of keys doesn't matter because MongoDB can traverse the index in either direction.

How many indexes does MongoDB create by default for a new collection?

MongoDB provides two geospatial indexes known as 2d indexes and 2d sphere indexes using these indexes we can query geospatial data.


1 Answers

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.

like image 147
styvane Avatar answered Nov 15 '22 17:11

styvane