Usage:
var uri = "mongodb://mongo:27017/"
var client = new MongoClient(uri)
assert client.server_uri == uri
mongodb :: MongoClient :: database_names
Lists available database names.mongodb :: MongoClient :: defaultinit
mongodb :: MongoClient :: last_error
Last error raised by mongoc.mongodb $ MongoClient :: SELF
Type of this instance, automatically specialized in every classmongodb $ MongoClient :: finalize_once
Real finalization method ofFinalizableOnce
, will be called only once
mongodb $ MongoClient :: init
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
mongodb :: MongoClient :: database_names
Lists available database names.core :: Object :: defaultinit
mongodb :: MongoClient :: defaultinit
core :: FinalizableOnce :: defaultinit
core :: Finalizable :: defaultinit
core :: Finalizable :: finalize
Liberate any resources held byself
before the memory holding self
is freed
core :: FinalizableOnce :: finalize_once
Real finalization method ofFinalizableOnce
, will be called only once
core :: FinalizableOnce :: finalized
Hasself
been finalized? (either by the GC or an explicit call to finalize
)
core :: FinalizableOnce :: finalized=
Hasself
been finalized? (either by the GC or an explicit call to finalize
)
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
mongodb :: MongoClient :: last_error
Last error raised by mongoc.core :: Object :: output_class_name
Display class name on stdout (debug only).
# The MongoClient is used to connect to the mongo server and send queries.
#
# Usage:
#
# ~~~
# var uri = "mongodb://mongo:27017/"
# var client = new MongoClient(uri)
# assert client.server_uri == uri
# ~~~
class MongoClient
super FinalizableOnce
# Server URI.
var server_uri: String
private var native: NativeMongoClient is noinit
init do native = new NativeMongoClient(server_uri.to_cstring)
# Gets server data.
#
# Returns `null` if an error occured. See `last_error`.
#
# ~~~
# var client = new MongoClient("mongodb://mongo:27017/")
# assert client.server_status["process"] == "mongod"
# ~~~
fun server_status: nullable JsonObject do
var nbson = native.server_status
if nbson == null then return null
var bson = new BSON(nbson)
var res = new JsonObject.from_bson(bson)
return res
end
# Lists available database names.
#
# ~~~
# var client = new MongoClient("mongodb://mongo:27017/")
# var db_suffix = "NIT_TESTING_ID".environ
# var db_name = "test_{db_suffix}"
# var db = client.database(db_name)
# db.collection("test").insert(new JsonObject)
# assert client.database_names.has(db_name)
# ~~~
fun database_names: Array[String] do
var res = new Array[String]
var nas = native.database_names
if nas == null then return res
var i = 0
var name = nas[i]
while not name.address_is_null do
res.add name.to_s
name.free
i += 1
name = nas[i]
end
return res
end
# Loads or creates a database from its `name`.
#
# Database are automatically created on the MongoDB server upon insertion of
# the first document into a collection.
# There is no need to create a database manually.
#
# ~~~
# var client = new MongoClient("mongodb://mongo:27017/")
# var db_suffix = "NIT_TESTING_ID".environ
# var db_name = "test_{db_suffix}"
# var db = client.database(db_name)
# assert db.name == db_name
# ~~~
fun database(name: String): MongoDb do return new MongoDb(self, name)
# Close the connexion and destroy the instance.
#
# The reference should not be used beyond this point!
fun close do finalize_once
redef fun finalize_once do native.destroy
# Last error raised by mongoc.
fun last_error: nullable MongoError do
var last_error = sys.last_mongoc_error
if last_error == null then return null
return new MongoError(last_error)
end
# Last auto generated id.
private fun last_id: nullable MongoObjectId do
var last_id = sys.last_mongoc_id
if last_id == null then return null
return new MongoObjectId.with_native(last_id)
end
# Set the last generated id or `null` to unset once used.
private fun last_id=(id: nullable MongoObjectId) do
if id == null then
sys.last_mongoc_id = null
else
sys.last_mongoc_id = id.native
end
end
end
lib/mongodb/mongodb.nit:195,1--299,3