我正在使用 MongoDB 1.6.3 来存储一个大集合(30 万多条记录)。我添加了一个复合索引。
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"
}
]
但是,当我尝试运行此代码时:
db['collection_name']
.find({:community_id => 1})
.sort(['updated_at.t', -1])
.skip(@skip)
.limit(@limit)
我得到:
Mongo::OperationFailure (too much data for sort() with no index. add an index or specify a smaller limit)
我做错了什么?
最佳答案
尝试添加 {community_id: 1, 'updated_at.t': -1}
索引。需要先按community_id
搜索再排序。
关于MongoDB - sort() 数据过多,没有索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4399068/