MongoConnection  
The MongoDB connection object
Updated Jul 30, 2011 by neoman...@gmail.com

Synopsis

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

Methods

db,err = mongo.Connection.New(t)

Returns a new Connection object, or nil and an error. Accepts an optional table of features:

  • auto_reconnect, (default = false)
  • rw_timeout, (default = 0, only supported in mongo >= v1.5)

ok,err = db:connect(connection_str)

ok,err = db:auth(t)

Authenticates a given database using a table of parameters:

  • 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,err = db:count(namespace)

ok,err = db:insert(namespace, value)

ok,err = db:insert(namespace, {value1, value2, ...})

Inserts multiple documents in one system call.

cursor,err = db:query(namespace, query)

TODO: document more options

ok,err = db:remove(namespace, query, single)

ok,err = db:update(namespace, query, modifier, upsert, multi)

is_failed = db:is_failed()

addr = db:get_server_address()

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


订阅 substack 体验古早写作:


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::