MongoDB Text和2D复合索引 [英] MongoDB Text and 2D compound index
问题描述
我试图这样做
db.users.ensureIndex({"books.title":"text", "location":"2d"})
MongoDB拒绝创建带有此错误的索引
and MongoDB refuses to create the index with this error
{
"ok" : 0,
"errmsg" : "bad index key pattern { books.title: \"text\", location: \"2d\" }: Can't use more than one index plugin for a single index.",
"code" : 67
}
在复合索引中文本索引不能与2d索引一起出现吗?
如果是这样,处理基于第一个books.title
字段然后基于location
字段的查询的可能的解决方法是什么?
Is it the case that text index cannot be together with 2d index in a compound index?
If so, what is the possible workaround to handle queries that are based on first books.title
field and then followed by location
field ?
谢谢
更新:
文本索引是必需的,因为我计划使用权重( http://docs.mongodb.org/manual/tutorial/control-results-of-text-search/),在不久的将来books.title
可能会变成books.title
,books.about
和
Text index is necessary because I plan to use weighting (http://docs.mongodb.org/manual/tutorial/control-results-of-text-search/) in the near future where books.title
may become books.title
, books.about
, and books.keywords
关于查询,将会是这样
db.users.find({"books.title": "eragon", location: { $near :[ 150 , 23 ]}})
这意味着,找到位置在我附近的用户,并拥有名为"eragon"的书
that means, find users whose location is near my point who have book entitled "eragon"
推荐答案
对于此类用例, $ geoWithin 可以代替$ near使用. $ geoWithin可以在没有2d/2dsphere索引的情况下工作.其他索引(文本索引)将用于根据文本搜索过滤结果,第二步$ geoWithin将根据位置过滤结果(无索引).
For such use cases, $geoWithin can be used instead of $near. $geoWithin can work without 2d/2dsphere index. Other index (text index) will be used to filter results based on text search and as second step $geoWithin will filter result (without index) based on location.
这篇关于MongoDB Text和2D复合索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!