Saves a new document in the collection.

If the document has an _id field it will be updated. Otherwise it will be inserted.

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.save(doc) # will be inserted
assert doc.has_key("_id")

var id = doc["_id"]
assert col.save(doc) # will be updated
assert doc["_id"] == id

Property definitions

mongodb $ MongoCollection :: save
	# Saves a new document in the collection.
	#
	# If the document has an `_id` field it will be updated.
	# Otherwise it will be inserted.
	#
	# 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.save(doc) # will be inserted
	# assert doc.has_key("_id")
	#
	# var id = doc["_id"]
	# assert col.save(doc) # will be updated
	# assert doc["_id"] == id
	# ~~~
	fun save(doc: JsonObject): Bool do
		var bson = doc.to_bson
		var nat = bson.native
		var res = native.save(nat)
		if res then set_id(doc)
		assert nat != self #FIXME used to avoid GC crashes
		assert bson != self #FIXME used to avoid GC crashes
		return res
	end
lib/mongodb/mongodb.nit:450,2--484,4