【发布时间】:2019-02-23 23:57:36
【问题描述】:
MongoDB 3.0.2。文档示例:
{ "_id" : NumberLong(1), Categories : { "123" : { "Shows" : NumberLong(7), "Clicks" : NumberLong(0) }, "221" : { "Shows" : NumberLong(33), "Clicks" : NumberLong(12) } } }
{ "_id" : NumberLong(2), Categories : { "221" : { "Shows" : NumberLong(33), "Clicks" : NumberLong(12) } } }
{ "_id" : NumberLong(3), Categories : { "123" : { "Shows" : NumberLong(8), "Clicks" : NumberLong(1) } } }
{ "_id" : NumberLong(4), Categories : { "99" : { "Shows" : NumberLong(144), "Clicks" : NumberLong(39) }, "221" : { "Shows" : NumberLong(52), "Clicks" : NumberLong(2) } } }
{ "_id" : NumberLong(5), Categories : { "95" : { "Shows" : NumberLong(18), "Clicks" : NumberLong(3) } } }
{ "_id" : NumberLong(6), Categories : { "123" : { "Shows" : NumberLong(89), "Clicks" : NumberLong(69) } } }
在我的例子中,像“123”、“221”、“99”、“95”这样的字符串值是一些类别的ID。我只需要使用类别 ID 按 Shows 进行排序:
db.myCollection.find().sort( Categories."1".Shows : -1 )
db.myCollection.find().sort( Categories."95".Shows : 1 )
db.myCollection.find().sort( Categories."123".Shows : 1 )
db.myCollection.find().sort( Categories."221".Shows : -1 )
但是这个类别的数量大约是 250+。我无法为每个设置索引,例如:
db.myCollection.createIndex( { Categories."[1..250]".Shows : 1 } );
因为 cat id 超过 64 个(MongoDB 限制),并且这个类别可以添加和删除到只是一些记录并且这个过程是动态的。
没有索引我得到:
带有消息的未捕获异常“MongoCursorException” 'localhost:27017:执行程序错误:溢出排序阶段缓冲数据 33554646 字节的使用量超过了 33554432 字节的内部限制..
有人可以为这种情况提供解决方案吗?
【问题讨论】:
标签: mongodb exception indexing multikey