Finds the first document that matches query.

Params:

  • skip number of documents to skip
  • limit number of documents to return

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)
var col = db.collection("test")
var query = new JsonObject
query["foo"] = 10
var doc = col.find(query)
assert doc["foo"] == 10

Property definitions

mongodb $ MongoCollection :: find
	# Finds the first document that matches `query`.
	#
	# Params:
	# * `skip` number of documents to skip
	# * `limit` number of documents to return
	#
	# 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)
	# var col = db.collection("test")
	# var query = new JsonObject
	# query["foo"] = 10
	# var doc = col.find(query)
	# assert doc["foo"] == 10
	# ~~~
	fun find(query: JsonObject, skip, limit: nullable Int): nullable JsonObject do
		var q = new NativeBSON.from_json_string(query.to_json.to_cstring)
		var s = skip or else 0
		var l = limit or else 0
		var c = native.find(q, s, l)
		q.destroy
		if c == null then return null
		var cursor = new MongoCursor(c)
		if not cursor.is_ok then
			return null
		end
		var item = cursor.item
		assert cursor != self
		return item
	end
lib/mongodb/mongodb.nit:560,2--593,4