Skip to content
Paco Zamora Martinez edited this page Dec 23, 2015 · 36 revisions

Methods

mongo.Connection.New{ [auto_reconnect=bool], [rw_timeout=int] } - Returns a new Connection object, or nil and an error.

db, err = mongo.Connection.New{}
  • auto_reconnect: (default = false) - attempt to reconnect if the database connection is lost
  • rw_timeout: (default = 0) - tcp timeout in seconds - this is for read/write, not connect.

connect(connection_str) - Connect to a mongo server, optionally specifying a port.

ok, err = db:connect([connection string]) 
  • connection_str: the database to which you want to connect - [hostname][:port number] e.g. "localhost:27018"

auth(dbname, username, password, digestPassword) - Authorize access to a particular database. Authentication is separate for each database on the server -- you may authenticate for any number of databases on a single connection. The 'admin' database is special and once authenticated provides access to all databases on the server.

ok, err = db:auth({dbname=[name], username=[username], password=[password], digestPassword=[true|false]})
  • dbname: database to authenticate (required)
  • username: username to authenticate against (required)
  • password: password to authenticate against (required)
  • digestPassword: if password is plain text, set this to true. otherwise assumed to be pre-digested

count(namespace) - count number of objects in collection described by namespace

count, err = db:count([namespace])
  • namespace - the database name and collection e.g. mydatabase.collection_name

insert(namespace, value) - inserts a document into a given collection

ok,err = db:insert([namespace], [table | JSON])
  • namespace - the database name and collection e.g. mydatabase.collection_name
  • value - a Lua table OR JSON string describing the document to insert

insert_batch - insert multiple documents into a given collection in one call

ok,err = db:insert_batch([namespace], [table | JSON])
  • value_n_ - a Lua table OR JSON string describing the documents to insert

query(namespace, query) - query a given database's collection

cursor,err = db:query([namespace], [table | JSON])
  • namespace - the database name and collection e.g. mydatabase.collection_name
  • query - a Lua table OR JSON string describing the query to execute

remove - remove documents in namespace matching query

ok,err = db:remove([namespace], [table | JSON], [true|false])
  • namespace - the database name and collection e.g. mydatabase.collection_name
  • query - a Lua table OR JSON string describing the query to execute
  • justOne - bool: remove only the first document found matching query

update(namespace, query, modifier, upsert, multi) - update a document in namespace matching query

ok,err = db:update([namespace], [table | JSON], [table | JSON], [bool], [bool])
  • namespace - the database name and collection e.g. mydatabase.collection_name
  • query - a Lua table OR JSON string describing the query to execute
  • modifier - a Lua table or JSON string describing the update
  • upsert - create a new document modifier if no documents match query
  • multi - if false, only the first document matching query will be updated

is_failed()

is_failed = db:is_failed()

get_server_address()

addr = db:get_server_address()

drop_collection(namespace)

ok,err = db:drop_collection([namespace])

drop_index_by_fields(namespace, fields)

ok,err = db:drop_index_by_fields([namespace], [table | JSON])

drop_index_by_name(namespace, name)

ok,err = db:drop_index_by_name([namespace], [index_name])

drop_indexes(namspace)

ok,err = db:drop_indexes([namespace])

exists(namespace)

bool = db:exists([namespace])

gen_index_name(fields)

name = db:gen_index_name([table | JSON])

enumerate_indexes(namespace)

cursor,err = db:enumerate_indexes([namespace])

mapreduce(namespace, map, reduce[, query[, output]])

res,err = db:mapreduce([namespace], [JS], [JS], [table | JSON], [string])
  • namespace - the database name and collection e.g. mydatabase.collection_name
  • map - a Javascript string implementing the map function
  • reduce - a Javascript string implementing the reduce function
  • query - a Lua table OR JSON string describing the query to execute
  • output - collection where output will be written

reindex(namespace)

ok,err = db:reindex([namespace])

get_last_error()

string = db:get_last_error([namespace])

get_last_error_detailed()

table = db:get_last_error_detailed([namespace])

run_command(namespace, command, options)

table = db:run_command([namespace], [table | JSON], [number])


get_dbnames()

res,err = db:get_dbnames()

get_collections([namespace])

res,err = db:get_collections([namespace])

Examples

local mongo = require('mongo')

-- Create a connection object
local db = assert(mongo.Connection.New())

-- connect to the server on localhost
assert(db:connect('localhost'))

-- insert a value into the namespace 'test.values'
assert(db:insert('test.values', {a = 10, b = 'str1'}))

-- the same using a JSON string
assert(db:insert('test.values', "{'a': 20, 'b': 'str2'}"))

-- insert a multiple values into the namespace 'test.values'
assert(db:insert_batch('test.values', {{a = 10, b = 'str1'}, {c = 11, d = 'str2'}}))

-- print the number of rows in the namespace 'test.values'
print(db:count('test.values'))

-- query all the values in the namespace 'test.values'
local q = assert(db:query('test.values', {}))

-- loop through the result set
for result in q:results() do
    print(result.a)
    print(result.b)
end

Orderby query example

> db = mongo.Connection.New()
> db:connect("localhost")
> db:insert("tmp.values", { a = 10, b = -1 })
> db:insert("tmp.values", { a = 20, b = -2 })
> db:insert("tmp.values", { a = 30, b = -3 })
> db:insert("tmp.values", { a = 1, b = 4 })
> c = assert( db:query("tmp.values", "{ query: {}, orderby: { a: 1 } }") )
> while c:has_more() do t = c:next() print(t.a, t.b) end
1	4
10	-1
20	-2
30	-3
> c = assert( db:query("tmp.values", "{ query: {}, orderby: { b: 1 } }") )
> while c:has_more() do t = c:next() print(t.a,t.b) end
30	-3
20	-2
10	-1
1	4

Clone this wiki locally