0

在不使用外部引擎的情况下,如何使用 mongodb 实现细粒度搜索?以这个对象为例 { 类型:'comedy',页数:380,年份:2013,畅销书:true,作者:'John Doe' }

正在通过以下方式搜索:

db.books.find({
  pages:      { '&gt': 100 },
  year:       { '&gt': 2000 },
  bestseller: true,
  author:     "John Doe"
});

到目前为止非常简单。现在假设文档中有更多的值,并且我正在进行更精确的搜索并且我有一个相当的集合。

我要做的第一件事是创建索引。但是,它是如何工作的?我已经读过这里定义的索引交集https://jira.mongodb.org/browse/SERVER-3071是不可行的。这意味着如果我将索引设置为“年份”和“页面”,我将不会真正优化搜索中的 AND 操作。

那么如何针对具有许多参数的搜索进行优化呢?

提前致谢。

4

1 回答 1

0

It seems like you are asking about compound indexes in mongodb. Compound indexes allow you to create a single index on multiple fields in a document. By creating compound indexes you can make these large/complex queries while still using an index.

On a more general note, if you create a basic index on a field that is highly selective, your search can end up being very quick. Using your example, if you had an index on author, the query engine would use that index to find all the entries where author == "John Doe". Presumably there are not that many books with that specific author relative to the number of books in the entire collection. So, even if the rest of your query is fairly complex, it is only evaluated over those few documents with the matching author. Thus, by structuring your indexes properly you can get a significant performance gain without having to have any complex indexes.

于 2013-03-26T20:39:35.130 回答