I don't understand a method of `MongoCollection`

15 views
Skip to first unread message

Freewind

unread,
Mar 20, 2011, 12:01:42 PM3/20/11
to mongo-scala-driver
I'm reading the source of mongo-scala-driver, there is a `getCount`
method in MongoCollection(line 51), I don't understand it:

protected def getCount(q: Query): Long = {
def lim(n: Int) = q.limit map { _ min n } getOrElse n
def skp(n: Int) = q.skip map { x => (n - x) max 0 } getOrElse n

if (q.slice_?) lim(skp(cursor(q).count))
else getCount(q.query)
}

Why not just `cursor(q).count` ?

Alexander Azarov

unread,
Mar 21, 2011, 3:04:51 AM3/21/11
to mongod...@googlegroups.com
See comment here https://github.com/osinka/mongo-scala-driver/blob/master/src/main/scala/com/osinka/mongodb/MongoCollection.scala#L212

It takes "limit" and "skip" into account. E.g. you have a collection with 10 documents, want to retrieve all of them and skip first 5. Mongo Java driver's `cursor(q).count` will return 10, but mongo-scala-driver's "coll.size" method will return 5 -- as this is what you'd expect, because you have a Scala collection.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages