user4951
user4951

Reputation: 33080

How to change mongodb index name

This is the name of the index I used

LongitudeLatitude_indexContents_1_prominent_-1

That -1 thingy is problematic and automatically generated. I want to change that to LongitudeLatitude_indexContents_1_prominent_min1

or something

Indexing takes a while. So i'd rather just change the name of the index.

For example,

If I do

db.tablebusiness.find({ "LongitudeLatitude" : { "$nearSphere" : [106.772835, -6.186753], "$maxDistance" : 0.044983732050783008 }, "Prominent" : { "$gte" : 15 }, "indexContents" : { "$all" : [/^warung/] } }).hint("LongitudeLatitude_indexContents_1_Prominent_-1").limit(50).explain();

I got this

> ).hint("LongitudeLatitude_indexContents_1_Prominent_-1").limit(50).explain();
Thu Sep 06 11:01:51 uncaught exception: error: { "$err" : "bad hint", "code" : 1
0113 }

Upvotes: 1

Views: 2734

Answers (2)

Sammaye
Sammaye

Reputation: 43884

Why are you using hint()? That has got to be some of the most unfuture proof code I have ever seen. If your query does not use the index effectively then you should really change the query or add another index to make the query run over the index. Forcing MongoDB to use an index it cannot find itself might actually slow your query further because it will attempt to use a b-tree that does not provide a shortcut.

Infact your query:

db.tablebusiness.find({ "LongitudeLatitude" : { "$nearSphere" : [106.772835, -6.186753], "$maxDistance" : 0.044983732050783008 }, "Prominent" : { "$gte" : 15 }, "indexContents" : { "$all" : [/^warung/] } })

Will use the index you created without using hint(). If you make a better index one day more effective for this query then using hint() will stop this query from being faster due to that new index. Not only that but you will have to change index names in all of your code.

As for changing the name of the index: I think you have to drop it first and then reindex creating a new index of your own name (though as @Thilo mentions, self named indexes are going to be removed).

Upvotes: 2

Thilo
Thilo

Reputation: 262494

.hint("LongitudeLatitude_indexContents_1_Prominent_-1")

should be

.hint({ LongitudeLatitude_indexContents: 1, Prominent: -1 })

Upvotes: 1

Related Questions