1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2015 Alexandre Terrasa <alexandre@moz-code.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 # This is actually a wrapper around the [MongoDB C Driver](http://api.mongodb.org/c/1.1.4/index.html).
24 # # Opens the connexion with the Mongo server.
25 # var client = new MongoClient("mongodb://localhost:27017/")
27 # # Retrieve a collection.
28 # var col = client.database("test").collection("test")
30 # # Insert a document in the collection.
31 # var doc = new JsonObject
34 # doc["baz"] = new JsonArray
35 # assert col.insert(doc)
37 # # Retrieve a document from the collection.
38 # var query = new JsonObject
40 # var res = col.find(query)
41 # assert res["bar"] == "bar"
47 private import native_mongodb
53 # Everything inside MongoDB is manipulated as BSON Objects.
56 # * [Binary JSON spec](http://bsonspec.org/)
57 # * [Libbson](http://api.mongodb.org/libbson/1.1.4/)#
61 # Native instance pointer.
62 var native
: NativeBSON
64 # Returns a new BSON object initialized from the content of `json`.
67 # intrude import mongodb
68 # var obj = new JsonObject
70 # obj["name"] = "Rick"
71 # obj["ELS"] = new JsonArray
72 # var bson = new BSON.from_json(obj)
73 # assert bson.to_s == """{ "age" : 10, "name" : "Rick", "ELS" : [ ] }"""
75 init from_json
(json
: JsonObject) do
76 init(new NativeBSON.from_json_string
(json
.to_json
.to_cstring
))
79 # Returns a new BSON object parsed from `json_string`.
81 # If `json_string` is not a valid JSON string, this initializer returns NULL.
84 # intrude import mongodb
85 # var str = """{ "age" : 10, "name" : "Rick", "ELS" : [ ] }"""
86 # var bson = new BSON.from_json_string(str)
87 # assert bson.to_s == str
89 init from_json_string
(json_string
: String) do
90 init(new NativeBSON.from_json_string
(json_string
.to_cstring
))
94 var ns
= native
.to_c_string
95 var res
= ns
.to_s_with_copy
96 ns
.free
# manual free of gc allocated CString
100 # Returns a new JsonObject from `self`.
103 # intrude import mongodb
104 # var str = """{ "age" : 10, "name" : "Rick", "ELS" : [ ] }"""
105 # var bson = new BSON.from_json_string(str)
106 # var json = bson.to_json
107 # assert json["age"] == 10
108 # assert json["name"] == "Rick"
109 # assert json["ELS"].as(JsonArray).is_empty
111 fun to_json
: JsonObject do
112 var json
= to_s
.parse_json
113 if json
isa JsonParseError then
117 return json
.as(JsonObject)
120 redef fun finalize_once
do native
.destroy
123 redef class JsonObject
124 # Inits `self` from a BSON object.
125 private init from_bson
(bson
: BSON) do add_all
(bson
.to_json
)
127 # Returns a new BSON object from `self`.
128 private fun to_bson
: BSON do return new BSON.from_json
(self)
131 # An error returned by the mongoc client.
133 # Within the client, if a method returns `false` or `null` it's more likely that
134 # an error occured during the execution.
136 # See `MongoClient::last_error`.
139 private var native
: BSONError
141 # Logical domain within a library that created the error.
142 fun domain
: Int do return native
.domain
144 # Domain specific error code.
145 fun code
: Int do return native
.code
147 # Human readable error message.
148 fun message
: String do
149 var ns
= native
.message
150 var res
= ns
.to_s_with_copy
155 redef fun to_s
do return "{message} (code: {code})"
158 # MongoDB Object ID representation.
160 # For ObjectIDs, MongoDB uses the `ObjectId("hash")` notation.
161 # This notation is replicated by the `to_s` service.
163 # Since the MongoDB notation is not JSON complient, the mongoc wrapper uses
164 # a JSON based notation like `{"$oid": "hash"}`.
165 # This is the notation returned by the `to_json` service.
168 private var native
: BSONObjectId = new BSONObjectId
170 private init with_native
(native
: BSONObjectId) do
174 # The unique ID as an MongoDB Object ID string.
175 fun id
: String do return native
.id
177 # Internal JSON representation of this Object ID.
179 # Something like `{"$oid": "5578e5dcf344225cc2378051"}`.
180 fun to_json
: JsonObject do
181 var obj
= new JsonObject
186 # Formatted as `ObjectId("5578e5dcf344225cc2378051")`
187 redef fun to_s
do return "ObjectId({id})"
190 # The MongoClient is used to connect to the mongo server and send queries.
195 # var uri = "mongodb://localhost:27017/"
196 # var client = new MongoClient(uri)
197 # assert client.server_uri == uri
200 super FinalizableOnce
203 var server_uri
: String
205 private var native
: NativeMongoClient is noinit
207 init do native
= new NativeMongoClient(server_uri
.to_cstring
)
211 # Returns `null` if an error occured. See `last_error`.
214 # var client = new MongoClient("mongodb://localhost:27017/")
215 # assert client.server_status["process"] == "mongod"
217 fun server_status
: nullable JsonObject do
218 var nbson
= native
.server_status
219 if nbson
== null then return null
220 var bson
= new BSON(nbson
)
221 var res
= new JsonObject.from_bson
(bson
)
225 # Lists available database names.
228 # var client = new MongoClient("mongodb://localhost:27017/")
229 # var db = client.database("test")
230 # db.collection("test").insert(new JsonObject)
231 # assert client.database_names.has("test")
233 fun database_names
: Array[String] do
234 var res
= new Array[String]
235 var nas
= native
.database_names
236 if nas
== null then return res
239 while not name
.address_is_null
do
240 res
.add name
.to_s_with_copy
248 # Loads or creates a database from its `name`.
250 # Database are automatically created on the MongoDB server upon insertion of
251 # the first document into a collection.
252 # There is no need to create a database manually.
255 # var client = new MongoClient("mongodb://localhost:27017/")
256 # assert client.database("test").name == "test"
258 fun database
(name
: String): MongoDb do return new MongoDb(self, name
)
260 # Close the connexion and destroy the instance.
262 # The reference should not be used beyond this point!
263 fun close
do finalize_once
265 redef fun finalize_once
do native
.destroy
267 # Last error raised by mongoc.
268 fun last_error
: nullable MongoError do
269 var last_error
= sys
.last_mongoc_error
270 if last_error
== null then return null
271 return new MongoError(last_error
)
274 # Last auto generated id.
275 private fun last_id
: nullable MongoObjectId do
276 var last_id
= sys
.last_mongoc_id
277 if last_id
== null then return null
278 return new MongoObjectId.with_native
(last_id
)
281 # Set the last generated id or `null` to unset once used.
282 private fun last_id
=(id
: nullable MongoObjectId) do
284 sys
.last_mongoc_id
= null
286 sys
.last_mongoc_id
= id
.native
291 # A MongoDb database.
293 # Database are automatically created on the MongoDB server upon insertion of the
294 # first document into a collection.
295 # There is no need to create a database manually.
297 super FinalizableOnce
299 # `MongoClient` used to load this database.
300 var client
: MongoClient
305 private var native
: NativeMongoDb is noinit
307 init do native
= new NativeMongoDb(client
.native
, name
.to_cstring
)
309 # Lists available collection names.
311 # Returns `null` if an error occured. See `Sys::last_mongoc_error`.
314 # var client = new MongoClient("mongodb://localhost:27017/")
315 # var db = client.database("test")
316 # db.collection("test").insert(new JsonObject)
317 # assert db.collection_names.has("test")
319 fun collection_names
: Array[String] do
320 var res
= new Array[String]
321 var nas
= native
.collection_names
322 if nas
== null then return res
325 while not name
.address_is_null
do
326 res
.add name
.to_s_with_copy
334 # Loads or creates a collection by its `name`.
337 # var client = new MongoClient("mongodb://localhost:27017/")
338 # var db = client.database("test")
339 # var col = db.collection("test")
340 # assert col.name == "test"
342 fun collection
(name
: String): MongoCollection do
343 return new MongoCollection(self, name
)
346 # Checks if a collection named `name` exists.
349 # var client = new MongoClient("mongodb://localhost:27017/")
350 # var db = client.database("test")
351 # assert not db.has_collection("qwerty")
353 fun has_collection
(name
: String): Bool do
355 return native
.has_collection
(name
.to_cstring
)
358 # Drop `self`, returns false if an error occured.
359 fun drop
: Bool do return native
.drop
361 redef fun finalize_once
do native
.destroy
364 # A Mongo collection.
366 # Collections are automatically created on the MongoDB server upon insertion of
367 # the first document.
368 # There is no need to create a database manually.
369 class MongoCollection
370 super FinalizableOnce
372 # Database that collection belongs to.
373 var database
: MongoDb
375 # Name of this collection.
378 private var native
: NativeMongoCollection is noinit
380 # Loads a collection.
382 # Call `MongoDb::collection` instead.
384 native
= new NativeMongoCollection(
385 database
.client
.native
,
386 database
.name
.to_cstring
,
390 # Set the autogenerated last id if the `doc` does not contain one already.
391 private fun set_id
(doc
: JsonObject) do
392 var last_id
= database
.client
.last_id
393 if last_id
!= null then
394 doc
["_id"] = last_id
.to_json
395 database
.client
.last_id
= null
399 # Inserts a new document in the collection.
401 # If no _id element is found in document, then a new one be generated locally
402 # and added to the document.
404 # Returns `false` if an error occured. See `Sys::last_mongoc_error`.
407 # var client = new MongoClient("mongodb://localhost:27017/")
408 # var col = client.database("test").collection("test")
409 # var doc = new JsonObject
412 # doc["baz"] = new JsonArray
413 # assert col.insert(doc)
414 # assert doc.has_key("_id")
416 fun insert
(doc
: JsonObject): Bool do
417 var res
= native
.insert
(doc
.to_bson
.native
)
418 if res
then set_id
(doc
)
422 # Inserts multiple documents in the collection.
425 fun insert_all
(docs
: Collection[JsonObject]): Bool do
427 for doc
in docs
do res
= insert
(doc
) and res
431 # Saves a new document in the collection.
433 # If the document has an `_id` field it will be updated.
434 # Otherwise it will be inserted.
436 # Returns `false` if an error occured. See `Sys::last_mongoc_error`.
439 # var client = new MongoClient("mongodb://localhost:27017/")
440 # var col = client.database("test").collection("test")
442 # var doc = new JsonObject
445 # doc["baz"] = new JsonArray
447 # assert col.save(doc) # will be inserted
448 # assert doc.has_key("_id")
450 # var id = doc["_id"]
451 # assert col.save(doc) # will be updated
452 # assert doc["_id"] == id
454 fun save
(doc
: JsonObject): Bool do
455 var bson
= doc
.to_bson
456 var nat
= bson
.native
457 var res
= native
.save
(nat
)
458 if res
then set_id
(doc
)
459 assert nat
!= self #FIXME used to avoid GC crashes
460 assert bson
!= self #FIXME used to avoid GC crashes
464 # Removes the first document that matches `selector`.
466 # Returns `false` if an error occured. See `Sys::last_mongoc_error`.
469 # var client = new MongoClient("mongodb://localhost:27017/")
470 # var col = client.database("test").collection("test")
471 # var sel = new JsonObject
473 # assert col.remove(sel)
475 fun remove
(selector
: JsonObject): Bool do
476 return native
.remove
(selector
.to_bson
.native
)
479 # Removes all the document that match `selector`.
482 fun remove_all
(selector
: JsonObject): Bool do
483 return native
.remove_all
(selector
.to_bson
.native
)
486 # Updates a document already existing in the collection.
488 # No upsert is done, see `save` instead.
491 # var client = new MongoClient("mongodb://localhost:27017/")
492 # var col = client.database("test").collection("test")
493 # var sel = new JsonObject
495 # var upd = new JsonObject
497 # assert col.update(sel, upd)
499 fun update
(selector
: JsonObject, update
: JsonObject): Bool do
500 return native
.update
(
501 selector
.to_bson
.native
,
502 update
.to_bson
.native
)
505 # Updates all documents matching the `selector`.
508 fun update_all
(selector
: JsonObject, update
: JsonObject): Bool do
509 return native
.update_all
(
510 selector
.to_bson
.native
,
511 update
.to_bson
.native
)
514 # Counts the document matching `query`.
516 # Returns `-1` if an error occured. See `Sys::last_mongoc_error`.
519 # var client = new MongoClient("mongodb://localhost:27017/")
520 # var col = client.database("test").collection("test")
521 # var query = new JsonObject
523 # assert col.count(query) > 0
525 fun count
(query
: JsonObject): Int do
526 return native
.count
(query
.to_bson
.native
)
529 # Finds the first document that matches `query`.
532 # * `skip` number of documents to skip
533 # * `limit` number of documents to return
535 # Returns `null` if an error occured. See `Sys::last_mongoc_error`.
538 # var client = new MongoClient("mongodb://localhost:27017/")
539 # var col = client.database("test").collection("test")
540 # var query = new JsonObject
542 # var doc = col.find(query)
543 # assert doc["foo"] == 10
545 fun find
(query
: JsonObject, skip
, limit
: nullable Int): nullable JsonObject do
546 var q
= new NativeBSON.from_json_string
(query
.to_json
.to_cstring
)
547 var s
= skip
or else 0
548 var l
= limit
or else 0
549 var c
= native
.find
(q
, s
, l
)
551 if c
== null then return null
552 var cursor
= new MongoCursor(c
)
553 if not cursor
.is_ok
then
556 var item
= cursor
.item
557 assert cursor
!= self
561 # Finds all the documents matching the `query`.
564 # * `skip` number of documents to skip
565 # * `limit` number of documents to return
568 # var client = new MongoClient("mongodb://localhost:27017/")
569 # var col = client.database("test").collection("test")
570 # var query = new JsonObject
572 # assert col.find_all(query).length > 0
574 fun find_all
(query
: JsonObject, skip
, limit
: nullable Int): Array[JsonObject] do
575 var s
= skip
or else 0
576 var l
= limit
or else 0
577 var res
= new Array[JsonObject]
578 var c
= native
.find
(query
.to_bson
.native
, s
, l
)
579 if c
== null then return res
580 var cursor
= new MongoCursor(c
)
581 while cursor
.is_ok
do
588 # Applies an aggregation `pipeline` over the collection.
591 # var client = new MongoClient("mongodb://localhost:27017/")
592 # var col = client.database("test").collection("test_aggregate")
596 # col.insert("""{ "cust_id": "A123", "amount": 500, "status": "A"}""".parse_json.as(JsonObject))
597 # col.insert("""{ "cust_id": "A123", "amount": 250, "status": "A"}""".parse_json.as(JsonObject))
598 # col.insert("""{ "cust_id": "B212", "amount": 200, "status": "A"}""".parse_json.as(JsonObject))
599 # col.insert("""{ "cust_id": "A123", "amount": 300, "status": "D"}""".parse_json.as(JsonObject))
601 # var res = col.aggregate("""[
602 # { "$match": { "status": "A" } },
603 # { "$group": { "_id": "$cust_id", "total": { "$sum": "$amount" } } }
604 # ]""".parse_json.as(JsonArray))
606 # assert res[0].to_json == """{"_id":"B212","total":200}"""
607 # assert res[1].to_json == """{"_id":"A123","total":750}"""
609 fun aggregate
(pipeline
: JsonArray): Array[JsonObject] do
610 var q
= new JsonObject
611 q
["pipeline"] = pipeline
612 var res
= new Array[JsonObject]
613 var c
= native
.aggregate
(q
.to_bson
.native
)
614 if c
== null then return res
615 var cursor
= new MongoCursor(c
)
616 while cursor
.is_ok
do
623 # Retrieves statistics about the collection.
625 # Returns `null` if an error occured. See `Sys::last_mongoc_error`.
628 # var client = new MongoClient("mongodb://localhost:27017/")
629 # var col = client.database("test").collection("test")
630 # assert col.stats["ns"] == "test.test"
632 fun stats
: nullable JsonObject do
633 var bson
= native
.stats
634 if bson
== null then return null
635 return new JsonObject.from_bson
(new BSON(bson
))
638 # Drops `self`, returns false if an error occured.
639 fun drop
: Bool do return native
.drop
641 # Moves `self` to another `database`.
643 # The database will also be updated internally so it is safe to continue using
644 # this collection after the move.
645 # Additional operations will occur on moved collection.
646 fun move
(database
: MongoDb): Bool do
647 self.database
= database
648 return native
.rename
(database
.name
.to_cstring
, name
.to_cstring
)
653 # The name of the collection will also be updated internally so it is safe
654 # to continue using this collection after the rename.
655 # Additional operations will occur on renamed collection.
656 fun rename
(name
: String): Bool do
658 return native
.rename
(database
.name
.to_cstring
, name
.to_cstring
)
661 redef fun finalize_once
do native
.destroy
664 # A MongoDB query cursor.
666 # It wraps up the wire protocol negotation required to initiate a query and
667 # retreive an unknown number of documents.
669 super FinalizableOnce
670 super Iterator[JsonObject]
672 private var native
: NativeMongoCursor
676 redef var is_ok
= true
678 redef fun next
do is_ok
= native
.next
681 return new JsonObject.from_bson
(new BSON(native
.current
))
684 redef fun finalize_once
do native
.destroy