Skip to content

Commit b3cc7d2

Browse files
committed
Merge: mongodb: allow client to set skip and limit when calling find
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org> Pull-Request: #1868 Reviewed-by: Romain Chanoir <romain.chanoir@viacesi.fr> Reviewed-by: Jean Privat <jean@pryen.org>
2 parents 0b30d25 + b085a88 commit b3cc7d2

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/mongodb/mongodb.nit

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ class MongoCollection
523523

524524
# Finds the first document that matches `query`.
525525
#
526+
# Params:
527+
# * `skip` number of documents to skip
528+
# * `limit` number of documents to return
529+
#
526530
# Returns `null` if an error occured. See `Sys::last_mongoc_error`.
527531
#
528532
# ~~~
@@ -533,9 +537,11 @@ class MongoCollection
533537
# var doc = col.find(query)
534538
# assert doc["foo"] == 10
535539
# ~~~
536-
fun find(query: JsonObject): nullable JsonObject do
540+
fun find(query: JsonObject, skip, limit: nullable Int): nullable JsonObject do
537541
var q = new NativeBSON.from_json_string(query.to_json.to_cstring)
538-
var c = native.find(q)
542+
var s = skip or else 0
543+
var l = limit or else 0
544+
var c = native.find(q, s, l)
539545
q.destroy
540546
if c == null then return null
541547
var cursor = new MongoCursor(c)
@@ -549,16 +555,22 @@ class MongoCollection
549555

550556
# Finds all the documents matching the `query`.
551557
#
558+
# Params:
559+
# * `skip` number of documents to skip
560+
# * `limit` number of documents to return
561+
#
552562
# ~~~
553563
# var client = new MongoClient("mongodb://localhost:27017/")
554564
# var col = client.database("test").collection("test")
555565
# var query = new JsonObject
556566
# query["foo"] = 10
557567
# assert col.find_all(query).length > 0
558568
# ~~~
559-
fun find_all(query: JsonObject): Array[JsonObject] do
569+
fun find_all(query: JsonObject, skip, limit: nullable Int): Array[JsonObject] do
570+
var s = skip or else 0
571+
var l = limit or else 0
560572
var res = new Array[JsonObject]
561-
var c = native.find(query.to_bson.native)
573+
var c = native.find(query.to_bson.native, s, l)
562574
if c == null then return res
563575
var cursor = new MongoCursor(c)
564576
while cursor.is_ok do

lib/mongodb/native_mongodb.nit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,11 @@ extern class NativeMongoCollection `{ mongoc_collection_t * `}
421421
#
422422
# If you would like to specify options such as a sort order,
423423
# the query must be placed inside of `{"$query": {}}`.
424-
fun find(query: NativeBSON): nullable NativeMongoCursor import
424+
fun find(query: NativeBSON, skip, limit: Int): nullable NativeMongoCursor import
425425
NativeMongoCursor.as nullable, set_mongoc_error `{
426426
bson_error_t error;
427427
mongoc_cursor_t *cursor;
428-
cursor = mongoc_collection_find(self, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
428+
cursor = mongoc_collection_find(self, MONGOC_QUERY_NONE, skip, limit, 0, query, NULL, NULL);
429429
if (mongoc_cursor_error(cursor, &error)) {
430430
NativeMongoCollection_set_mongoc_error(self, &error);
431431
return null_NativeMongoCursor();

0 commit comments

Comments
 (0)