本文介绍了MongoDB - sort() 数据过多,没有索引错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 MongoDB 1.6.3 来存储一个大集合(30 万多条记录).我添加了一个复合索引.
I am using MongoDB 1.6.3, to store a big collection (300k+ records). I added a composite index.
db['collection_name'].getIndexes()
[
{
"name" : "_id_",
"ns" : "db_name.event_logs",
"key" : {
"_id" : 1
}
},
{
"key" : {
"updated_at.t" : -1,
"community_id" : 1
},
"ns" : "db_name.event_logs",
"background" : true,
"name" : "updated_at.t_-1_community_id_1"
}
]
但是,当我尝试运行此代码时:
However, when I try to run this code:
db['collection_name']
.find({:community_id => 1})
.sort(['updated_at.t', -1])
.skip(@skip)
.limit(@limit)
我得到:
Mongo::OperationFailure(数据过多对于没有索引的 sort().添加一个索引或指定更小的限制)
我做错了什么?
推荐答案
尝试添加 {community_id: 1, 'updated_at.t': -1}
索引.需要先按community_id
搜索再排序.
Try adding {community_id: 1, 'updated_at.t': -1}
index. It needs to search by community_id
first and then sort.
这篇关于MongoDB - sort() 数据过多,没有索引错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!