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.
MongoClient
used to load this database.
mongodb :: MongoDb :: collection
Loads or creates a collection by itsname
.
mongodb :: MongoDb :: collection_names
Lists available collection names.mongodb :: MongoDb :: defaultinit
mongodb :: MongoDb :: has_collection
Checks if a collection namedname
exists.
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
MongoClient
used to load this database.
mongodb :: MongoDb :: collection
Loads or creates a collection by itsname
.
mongodb :: MongoDb :: collection_names
Lists available collection names.core :: Object :: defaultinit
core :: FinalizableOnce :: defaultinit
mongodb :: MongoDb :: 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
)
mongodb :: MongoDb :: has_collection
Checks if a collection namedname
exists.
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.
core :: Object :: output_class_name
Display class name on stdout (debug only).
# A MongoDb database.
#
# 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.
class MongoDb
super FinalizableOnce
# `MongoClient` used to load this database.
var client: MongoClient
# The database name.
var name: String
private var native: NativeMongoDb is noinit
init do native = new NativeMongoDb(client.native, name.to_cstring)
# Lists available collection names.
#
# Returns `null` if an error occured. See `Sys::last_mongoc_error`.
#
# ~~~
# 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 db.collection_names.has("test")
# ~~~
fun collection_names: Array[String] do
var res = new Array[String]
var nas = native.collection_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 collection by its `name`.
#
# ~~~
# 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)
# var col = db.collection("test")
# assert col.name == "test"
# ~~~
fun collection(name: String): MongoCollection do
return new MongoCollection(self, name)
end
# Checks if a collection named `name` exists.
#
# ~~~
# 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 not db.has_collection("qwerty")
# ~~~
fun has_collection(name: String): Bool do
# TODO handle error
return native.has_collection(name.to_cstring)
end
# Drop `self`, returns false if an error occured.
fun drop: Bool do return native.drop
redef fun finalize_once do native.destroy
end
lib/mongodb/mongodb.nit:301,1--378,3