小编Dou*_*aul的帖子

是否可以以索引支持的方式在同一个数组上执行“$unwind”和“$sort”?

我正在尝试使用聚合框架来展开数组并获取按数组中子文档的字段排序的结果。有没有办法做到这一点,以便可以使用索引进行排序?

例如,假设我有一个这样的文档数据库:

{
  name: "Alan",
  attempts: [{
    assessmentName: "Test 1",
    score: 87
  }, {
    assessmentName: "Test 2",
    score: 62
  }]
}
Run Code Online (Sandbox Code Playgroud)

以及分数索引,{'attempts.score': 1}

我想要一个聚合管道,结果是:

{ name: "Alan", attempts: {assessmentName: "Test 2", score: 62} }
{ name: "Alan", attempts: {assessmentName: "Test 1", score: 87} }
...
Run Code Online (Sandbox Code Playgroud)

如果我把$sort舞台放在第一位:

db.students.aggregate([
  {$sort: {'attempts.score': 1}},
  {$unwind: '$attempts'}
])
Run Code Online (Sandbox Code Playgroud)

那么我的索引可以使用,但结果当然并没有在$unwind.

如果我把$sort舞台放在第二位:

db.students.aggregate([
  {$unwind: '$attempts'},
  {$sort: {'attempts.score': 1}}
])
Run Code Online (Sandbox Code Playgroud)

然后我会得到我想要的结果,但对于大型集合来说这是不可行的,因为我的索引无法使用。(Mongo 中止并显示“排序超出了 104857600 字节的内存限制,但没有选择加入外部排序。中止操作。传递 allowedDiskUse:true 以选择加入。”允许它使用磁盘可以避免该错误,但不会满足我的要求性能要求。)

根据我迄今为止的研究,我怀疑聚合框架目前不可能实现这一点。但我当然希望我错了。

针对一些评论的更新:当然可以通过更改数据模型来避免这一挑战,但为了解决这个问题,我们假设这是我们所坚持的数据模型。(在很多情况下,更改数据模型并不是一个可行的解决方案,至少在短期内不是。)

另外,我问这个问题的部分原因是我认为 Mongo …

mongodb aggregation-framework

5
推荐指数
0
解决办法
1475
查看次数

如何避免DatabaseObjectNotClosedException

我们如何避免这种异常?

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): Releasing statement in a finalizer.    Please ensure that you explicitly call close() on your cursor: SELECT * FROM test

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):     at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:62)

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):     at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80)

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):     at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:46)

android

2
推荐指数
1
解决办法
8001
查看次数

标签 统计

aggregation-framework ×1

android ×1

mongodb ×1