mongodb :: MongoCollection :: insert
If no _id element is found in document, then a new one be generated locally and added to the document.
Returns false
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)
var col = db.collection("test")
var doc = new JsonObject
doc["foo"] = 10
doc["bar"] = "bar"
doc["baz"] = new JsonArray
assert col.insert(doc)
assert doc.has_key("_id")
# Inserts a new document in the collection.
#
# If no _id element is found in document, then a new one be generated locally
# and added to the document.
#
# Returns `false` 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)
# var col = db.collection("test")
# var doc = new JsonObject
# doc["foo"] = 10
# doc["bar"] = "bar"
# doc["baz"] = new JsonArray
# assert col.insert(doc)
# assert doc.has_key("_id")
# ~~~
fun insert(doc: JsonObject): Bool do
var res = native.insert(doc.to_bson.native)
if res then set_id(doc)
return res
end
lib/mongodb/mongodb.nit:415,2--439,4