ReactiveMongo无法以排序方式下降

时间:2016-01-08 19:52:46

标签: mongodb

对Scala 0.11使用ReactiveMongo 2.11。我有一个问题,我的查询无法下降。以下是我的Index和ReactiveMongo查询:

collection.indexesManager.ensure(Index(Seq("userId" -> IndexType.Ascending, "lastActivity" -> IndexType.Descending), background = true))

def listEfforts(userId: String, page: Int, pageSize: Int): Future[\/[ErrMsg, List[EffortDesc]]] = {
      val query = BSONDocument("userId" -> userId)
      val sort = BSONDocument("lastActivity" -> -1)
      val skipN = (page - 1) * pageSize
      val queryOptions = new QueryOpts(skipN = skipN, batchSizeN = pageSize, flagsN = 0)

      collection.find(query).options(queryOptions).sort(sort).cursor[EffortDesc](ReadPreference.primaryPreferred).collect[List](pageSize).flatMap {
        case list => Future(\/.right(list))
      }
  }

即使我的排序变量已设置为-1,我的结果仍然是提升的。 lastActivity是一个Unix时间戳字段,以毫秒为单位。我已经尝试过其他调试问题(比如重新编译等)

知道可能导致这种情况的原因是什么?谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

发现了这个问题。如果我在lastActivity字段上放置一个IndexType.Descending,然后另外排序为"降序" (通过"lastActivity" -> -1)MongoDB将首先根据索引返回降序排序,然后再对其进行排序。

我不确定这是否是Mongo中的正常/预期行为,但将-1更改为1解决了问题。

答案 1 :(得分:0)

使用

from threading import Timer import time class InfiniteTimer(): """A Timer class that does not stop, unless you want it to.""" def __init__(self, seconds, target): self._should_continue = False self.is_running = False self.seconds = seconds self.target = target self.thread = None def _handle_target(self): self.is_running = True self.target() self.is_running = False self._start_timer() def _start_timer(self): if self._should_continue: # Code could have been running when cancel was called. self.thread = Timer(self.seconds, self._handle_target) self.thread.start() def start(self): if not self._should_continue and not self.is_running: self._should_continue = True self._start_timer() else: print("Timer already started or running, please wait if you're restarting.") def cancel(self): if self.thread is not None: self._should_continue = False # Just in case thread is running and cancel fails. self.thread.cancel() else: print("Timer never started or failed to initialize.") def tick(): print('ipsem lorem') # Example Usage t = InfiniteTimer(0.5, tick) t.start()

("fieldName", BSONInteger.apply(1))

适合我。