如何使用 MongoMapper 精确控制查询
我的数据库中有一个这样的索引(用于我的表条目): {"created_at": -1, "search_id": 1, "services":1}
我可以使用该索引如果我在 Mongo 控制台中执行搜索,如下所示:(
db.entries.find({'search_id':1, 'services':2}).sort({'created_at':
-1})
如果我对该查询执行explain(),我可以看到它正在使用索引)。
但是我无法使用 MongoMapper 复制该行为。我尝试过这样的搜索:
Entry.where(:search_id => 1, :services => 2).order(['created_at',
'descending'])
我改变了顺序并尝试了一切,但是当我对其进行解释时,我发现它使用的是 BasicCursor (即没有索引),并且如果我执行“条件”在该查询中,我可以看到它甚至没有尝试对搜索中的结果进行排序:我猜一旦结果返回,它们就会被排序。
有没有一种方法可以更精确地控制您的查询,以便您可以在此类情况下使用索引?
I have an index like this in my database (for my table entries): {"created_at": -1, "search_id": 1, "services":1}
I can make use of that index if I perform a search in the Mongo console like this:
db.entries.find({'search_id':1, 'services':2}).sort({'created_at':
-1})
(if I perform an explain() on that query I can see that it's using the index).
However I can't replicate that behavior with MongoMapper. I've tried with a search like this:
Entry.where(:search_id => 1, :services => 2).order(['created_at',
'descending'])
I've altered the order and tried everything, but when I perform an explain on it I get that it's using a BasicCursor (that is, no index) and if I do a "criteria" on that query I can see that it's not even trying to order the results in the search: I guess they are ordered afterwards once the results are back.
Is there a way to control your queries more precisely so you can make use of an index in cases like these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的坏家伙,我错误地使用了 order 函数,因为这次它需要一个符号而不是一个数组:
My bad guys, I was using the
order
function incorrectly as this time it expects a symbol rather than an array: