1

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 }
4
  • 2
    Why is the name a problem? The MongoDB docs state that the option to assign names to indexes will be removed in the future (leaving only the auto-generated default names), so you may have to rethink this. Commented Sep 6, 2012 at 4:03
  • Take your finger off the index names. The are for very good reasons generated automatically. Commented Sep 6, 2012 at 4:22
  • Read the question again > ).hint("LongitudeLatitude_indexContents_1_Prominent_-1").limit(50).explain(); Thu Sep 06 11:01:51 uncaught exception: error: { "$err" : "bad hint", "code" : 1 0113 } How do I solve that? Commented Sep 6, 2012 at 4:44
  • That is fine. However, variable names usually must not contain -1. That's the issue. Indexes are like variable names right? Commented Sep 7, 2012 at 8:28

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

9 Comments

That is fine. However, variable names usually must not contain -1. That's the issue. Indexes are like variable names right?
@JimThio In what sense? I have feeling I am about to say no. They are actually keys within an array stored within Mongo b-tree mapper. So when you hint() an index you are hinting a string not a variable name.
@JimThio I noticed the name of your index is: LongitudeLatitude_indexContents_1_prominent_-1 and you are using LongitudeLatitude_indexContents_1_Prominent_-1 I believe the index names are case sensitive, try the same case as you see when you do getIndexes()
That could have been the problem. Eventually it works though and I don't know why. That could be the issue?
@JimThio Hmm that does kinda cofuse me, so no code or db change and it started working?
|
1

.hint("LongitudeLatitude_indexContents_1_Prominent_-1")

should be

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

7 Comments

or maybe it is just the capitalization of "P"
Why? I want to force mongodb to use LongitudeLatitude_indexContents_1_Prominent_-1 index so I specify it by name.
It is not necessary to specify it by name, you can use the other syntax. Also, are you sure that the index name (and field name) has a capital "P" in there?
What about if you have SEVERAL indexes with longitudelatitude, indexcontents, and prominent? For example, longitudelatitude may be indexed as 2d and may also use geohaystack.
The index hint has to match the index definition, including field order. I don't think this will become ambiguous.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.