Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

MongoConnection

pakozm edited this page · 35 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"

db: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

db: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()

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

Something went wrong with that request. Please try again.