简体   繁体   English

如何更改mongodb索引名称

[英]How to change mongodb index name

This is the name of the index I used 这是我使用的索引的名称

LongitudeLatitude_indexContents_1_prominent_-1 LongitudeLatitude_indexContents_1_prominent_-1

That -1 thingy is problematic and automatically generated. -1th的问题是有问题的,并自动生成。 I want to change that to LongitudeLatitude_indexContents_1_prominent_min1 我想将其更改为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 }

Why are you using hint() ? 你为什么要使用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. 强制MongoDB使用无法找到的索引实际上可能会进一步降低查询速度,因为它将尝试使用不提供快捷方式的b树。

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() . 将使用您创建的索引,而无需使用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. 如果您对这一查询的索引更好地提高了一天的查询效率,则由于使用了新的索引,因此使用hint()将使此查询不再更快。 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). 至于更改索引的名称:我认为您必须先删除它,然后重新索引以创建自己名称的新索引(尽管如@Thilo所述,自命名索引将被删除)。

.hint("LongitudeLatitude_indexContents_1_Prominent_-1") .hint(“ LongitudeLatitude_indexContents_1_Prominent_-1”)

should be 应该

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM