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按显示进行排序:
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字节。
有人可以为这种情况提供解决方案吗?
答案 0 :(得分:0)
我认为您无法使用当前的文档设计解决此问题。
您可能希望以其他方式为类别建模,例如对类别使用数组,如下所示:
{
"_id" : 1,
"Categories": [
{
"CategoryId": "123",
"Details": {
"Shows": 7,
"Clicks": 0
}, {
"CategoryId": "221",
"Details": {
"Shows": 33,
"Clicks": 12
}
}
]
}
在这种情况下,您可以轻松添加单个索引db.collection.createIndex({"Categories.Details.Shows": 1})
答案 1 :(得分:0)
Using a slightly modified version of dnickless's schema as shown below: { "_id" : 1, "Categories": [ { "CategoryId": "123", "Shows": 7, "Clicks": 0 }, { "CategoryId": "221", "Shows": 33, "Clicks": 12 } ] } You could then create an index on shows. db.collection.createIndex({"Categories.Shows": 1})