readme: add information section
[nit.git] / lib / mongodb / mongodb.nit
index 6a8727e..78cd75d 100644 (file)
@@ -110,7 +110,6 @@ private class BSON
        fun to_json: JsonObject do
                var json = to_s.parse_json
                if json isa JsonParseError then
-                       print to_s
                        print json.message
                        sys.exit 1
                end
@@ -524,6 +523,10 @@ class MongoCollection
 
        # Finds the first document that matches `query`.
        #
+       # Params:
+       # * `skip` number of documents to skip
+       # * `limit` number of documents to return
+       #
        # Returns `null` if an error occured. See `Sys::last_mongoc_error`.
        #
        # ~~~
@@ -534,9 +537,11 @@ class MongoCollection
        # var doc = col.find(query)
        # assert doc["foo"] == 10
        # ~~~
-       fun find(query: JsonObject): nullable JsonObject do
+       fun find(query: JsonObject, skip, limit: nullable Int): nullable JsonObject do
                var q = new NativeBSON.from_json_string(query.to_json.to_cstring)
-               var c = native.find(q)
+               var s = skip or else 0
+               var l = limit or else 0
+               var c = native.find(q, s, l)
                q.destroy
                if c == null then return null
                var cursor = new MongoCursor(c)
@@ -550,6 +555,10 @@ class MongoCollection
 
        # Finds all the documents matching the `query`.
        #
+       # Params:
+       # * `skip` number of documents to skip
+       # * `limit` number of documents to return
+       #
        # ~~~
        # var client = new MongoClient("mongodb://localhost:27017/")
        # var col = client.database("test").collection("test")
@@ -557,12 +566,17 @@ class MongoCollection
        # query["foo"] = 10
        # assert col.find_all(query).length > 0
        # ~~~
-       fun find_all(query: JsonObject): Array[JsonObject] do
+       fun find_all(query: JsonObject, skip, limit: nullable Int): Array[JsonObject] do
+               var s = skip or else 0
+               var l = limit or else 0
                var res = new Array[JsonObject]
-               var c = native.find(query.to_bson.native)
+               var c = native.find(query.to_bson.native, s, l)
                if c == null then return res
                var cursor = new MongoCursor(c)
-               for item in cursor do res.add item
+               while cursor.is_ok do
+                       res.add cursor.item
+                       cursor.next
+               end
                return res
        end
 
@@ -619,9 +633,9 @@ class MongoCursor
 
        init do next
 
-       redef fun is_ok do return native.more
+       redef var is_ok = true
 
-       redef fun next do native.next
+       redef fun next do is_ok = native.next
 
        redef fun item do
                return new JsonObject.from_bson(new BSON(native.current))