This is a pure luvit Mongodb driver with async Api.
Luvit is a Node.js like, with Asynchronous I/O support language for Lua.
Learn more a: Luvit
-
With lit:
Run
lit install cyrilis/luvit-mongodb
in terminal under your project path, then
require("luvit-mongodb")
in your project. -
Just put source file in your project, then require it with
./path/to/luvit-mongodb
local Mongo = require("luvit-mongodb")
local db = Mongo:new({db = "DATABASE_NAME"})
-- replace `DATABASE_NAME` above with a DB name.
db:on("connect", function()
-- Do stuff here.
local Post = db:collection("post")
local page = 1
Post:insert({
title = "Hello word!",
content = "Here is the first blog post ....",
author = "Cyril Hou"
},function(err, res)
p(res)
end)
local posts = Post:find({author = "Cyril Hou"})
posts:limit(10):skip(page * 10):update({authorAge = 25}):exec(function(err, res)
p(err, res)
end)
Post:distinct("category", function(err, res)
p("All distinct value of `category` in post collections: ", res)
end)
end)
-
cursor:update(doc[, callback])
doc
: table, required, The modifications to apply.callback
: optional, callback function
post:update({abc = 123}, function(err, res) p(err, res) end) -- same as bellow ⬇️ post:update({abc = 123}):exec(function(err, res) p(err, res) end)
-
cursor:find(query[, callback])
query
: table, optional , Specifies selection criteria using query operators. To return all documents in a collection, omit this parameter or pass an empty document ({}).callback
: optional, callback function
post:find({abc = 123}, function(err, res) p(err, res) end) -- same as bellow ⬇️ post:find({abc = 123}):exec(function(err, res) p(err, res) end)
-
cursor:remove([callback])
callback
: optional, callback function
post:remove(function(err, res) p(err, res) end) -- same as bellow ⬇️ post:remove():exec(function(err, res) p(err, res) end)
-
cursor:skip(skip[, callback])
skip
: number, docs to skip in query.callback
: optional, callback function
-
cursor:limit(limit[, callback])
limit
: number, limit return docs count.callback
: optional, callback function
-
cursor:sort(doc[, callback])
doc
: table,callback
: optional, callback function
Example:
local User = db:collection("user") User:find():sort({age = 1}):exec(function(err, res) p(err, res) end)
-
cursor:count([callback])
callback
: optional, callback function
post:count(function(err, res) p(err, res) end) -- same as bellow ⬇️ post:remove():exec(function(err, res) p(err, res) end)
-
cursor:exec(callback)
-
callback
: required, callback functionExecute callback function after set query and limit skip params.
-
-
A collection should initialize with db instance.
db:collection(collectionName)
- collectionName: string, name for collection in name.
local coll = db:collection("post")
-
coll:find(query[, callback])
- query: table, optional, Specifies selection criteria using query operators. To return all documents in a collection, omit this parameter or pass an empty document ({}).
- callback: function, optional, callback function.
coll:find({id = 1}):exec(function(err, res) p(err, res) end) -- same as bellow ⬇️ coll:find({id = 1}, function(err, res) p(err, res) end)
`coll:distinct(field[, query][, callback])`
-
field: string, required, The field for which to return distinct values.
-
query: table, optional, A query that specifies the documents from which to retrieve the distinct values.
-
callback: function, optional, Callback function.
coll:distinct("category", {public = true}, function(err, res) p(err, res) end)
-
coll:drop(callback)
- callback: function, required, callback function
coll:drop(function(err, res) p(err, res) end)
-
coll:findAndModify(query, update[, callback])
- query: table, required, Optional. The selection criteria for the modification. The query field employs the same query selectors as used in the
collection.find()
method. Although the query may match multiple documents,findAndModify()
will only select one document to modify. - update: table, required, The update field employs the same update operators or field: value specifications to modify the selected document.
- callback: function, optional, Callback function.
coll:findAndModify({abc = 123}, {def = true}, function(err, res) p(err, res) end)
- query: table, required, Optional. The selection criteria for the modification. The query field employs the same query selectors as used in the
-
coll:findOne(query[, callback])
- query: table, required, Optional. The selection criteria for the modification. The query field employs the same query selectors as used in the
collection.find()
method. Although the query may match multiple documents,findAndModify()
will only select one document to modify. - callback: function, optional, Callback function.
coll:findOne({abc = 123}, function(err, res) p(err, res) end)
- query: table, required, Optional. The selection criteria for the modification. The query field employs the same query selectors as used in the
-
coll:remove(query[, callback])
- query: table, required, Optional. The selection criteria for the modification. The query field employs the same query selectors as used in the
collection.find()
method - callback: function, optional, Callback function.
coll:remove({public = false}, function(err, res) p(err, res) end)
- query: table, required, Optional. The selection criteria for the modification. The query field employs the same query selectors as used in the
-
coll:insert(doc[, callback])
- doc: table, required, A document or array of documents to insert into the collection.
- callback: function, optional, Callback function.
coll:insert({title = "Hello World!"}, function(err, res) p(err, res) end)
coll:find():update({public = true}):exec(function(err, res)
p(err, res)
end)
-
`db:insert(collection, document, continue, callback)` - `collection`: Database collection name, **required** - `document`: lua table, **required**
continue
: if continue when error occuredcallback
: function, with inserted document as #1 parameter
-
`db:find(collection, query, fields, skip, limit, callback)`
collection
: Database collection name, requiredquery
: mongodb query eg:{ type: { $in: [ 'food', 'snacks' ] } }
or just{ type: "snacks" }
, requiredfields
: limit field for return, eg:{ item: 1, qty: 1, _id:0 }
skip
: skip counts, intlimit
: limit return counts, intcallback
: function, with found documents array as #1 parameter
-
`db:update(collection, query, update, upsert, single, callback)` - `collection`: Database collection name, **required**
query
: mongodb query eg:{ type: { $in: [ 'food', 'snacks' ] } }
or just{ type: "snacks" }
, requiredupdate
: mongodb update, eg:{ $set: { "detail": "14Q2" } }
, requiredupsert
: if not find result withquery
params, insert a record withupdate
params.single
: update multiple records or single record, boolean.callback
: function, without parameters
-
`(collection, query, single, callback)` - `collection`: Database collection name, **required**
-
query
: mongodb query eg:{ type: { $in: [ 'food', 'snacks' ] } }
or just{ type: "snacks" }
, required -
single
: remove multiple records or single, boolean.- `callback`: function, without parameters
-
-
`(collection, query, callback)` - `collection`: Database collection name, **required**
-
query
: mongodb query eg:{ type: { $in: [ 'food', 'snacks' ] } }
or just{ type: "snacks" }
, required- `callback`: function, without parameters
-
-
`db:find(collection, query, fields, skip, callback)`
collection
: Database collection name, requiredquery
: mongodb query eg:{ type: { $in: [ 'food', 'snacks' ] } }
or just{ type: "snacks" }
, requiredfields
: limit field for return, eg:{ item: 1, qty: 1, _id:0 }
skip
: skip counts, intcallback
: function, without found document as #1 parameter
See files in "test/" Folder
If you have any issue while use this library please let me know. thanks.
- Bit64 support
- Bit32 support
- Date type support
- Cursor
- Raw Command support
- Write concern
- Auth
- Create index
- Get index
- Remove index
The MIT License (MIT)
Copyright (c) 2015 Cyril Hou<[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.