All Projects → neoxic → Lua Mongo

neoxic / Lua Mongo

Licence: mit
MongoDB Driver for Lua

Programming Languages

c
50402 projects - #5 most used programming language
lua
6591 projects

Projects that are alternatives of or similar to Lua Mongo

Avocado
Strongly-typed MongoDB driver for Rust
Stars: ✭ 70 (-13.58%)
Mutual labels:  bson, mongo, mongodb-driver, mongodb
Mongo Cxx Driver
C++ Driver for MongoDB
Stars: ✭ 792 (+877.78%)
Mutual labels:  bson, mongodb-driver, mongodb
Lungo
A MongoDB compatible embeddable database and toolkit for Go.
Stars: ✭ 343 (+323.46%)
Mutual labels:  bson, mongo, mongodb
Mongo Php Driver
MongoDB PHP driver
Stars: ✭ 737 (+809.88%)
Mutual labels:  mongodb-driver, mongodb
Laravel Mongodb
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
Stars: ✭ 5,860 (+7134.57%)
Mutual labels:  mongo, mongodb
Alcinoe
Alcinoe Component Library For Delphi. Full opengl video player, WebRTC delphi wrapper, native ios/android TEdit, Improuved firemonkey controls, Firebase cloud messaging, Android/ios facebook sdk login, Json/Bson Parser, ImageMagick wrapper, MongoDb client And much more
Stars: ✭ 657 (+711.11%)
Mutual labels:  bson, mongodb-driver
Migrate Mongo
A database migration tool for MongoDB in Node
Stars: ✭ 481 (+493.83%)
Mutual labels:  mongo, mongodb
Node Production
Take Your Node.js Project to The Production Environment (VPS/Dedicated Server).
Stars: ✭ 35 (-56.79%)
Mutual labels:  mongo, mongodb
Mondocks
An alternative way to interact with MongoDB databases from F# that allows you to use mongo-idiomatic constructs
Stars: ✭ 20 (-75.31%)
Mutual labels:  mongo, mongodb
Mongoku
🔥The Web-scale GUI for MongoDB
Stars: ✭ 1,000 (+1134.57%)
Mutual labels:  mongo, mongodb
Mongoc.jl
MongoDB driver for the Julia Language
Stars: ✭ 46 (-43.21%)
Mutual labels:  mongodb-driver, mongodb
Mongo Rust Driver
The official MongoDB Rust Driver
Stars: ✭ 633 (+681.48%)
Mutual labels:  mongodb-driver, mongodb
Mongokitten
Native MongoDB driver for Swift, written in Swift
Stars: ✭ 605 (+646.91%)
Mutual labels:  mongodb-driver, mongodb
Denodb
MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
Stars: ✭ 498 (+514.81%)
Mutual labels:  mongo, mongodb
Phalcon Mongodb Odm
MongoDB ODM for Phalcon framework for new mongodb php extension with query builder and rich functionality
Stars: ✭ 42 (-48.15%)
Mutual labels:  mongodb-driver, mongodb
Mern Stack Authentication
Secure MERN Stack CRUD Web Application using Passport.js Authentication
Stars: ✭ 60 (-25.93%)
Mutual labels:  mongo, mongodb
Mean Angular5 Passport Authentication
Securing MEAN Stack (Angular 5) Web Application using Passport Authentication
Stars: ✭ 24 (-70.37%)
Mutual labels:  mongo, mongodb
Spruce
A social networking platform made using Node.js and MongoDB
Stars: ✭ 399 (+392.59%)
Mutual labels:  mongo, mongodb
Qmgo
Qmgo - The Go driver for MongoDB. It‘s based on official mongo-go-driver but easier to use like Mgo.
Stars: ✭ 444 (+448.15%)
Mutual labels:  mongodb-driver, mongodb
Mean Angular4 Chat App
MEAN stack with Angular 4 Chat App
Stars: ✭ 41 (-49.38%)
Mutual labels:  mongo, mongodb

MongoDB Driver for Lua

lua-mongo is a binding to MongoDB C Driver 1.16 or higher for Lua:

  • Unified API for MongoDB commands, CRUD operations and GridFS in MongoDB C Driver.
  • Support for data transformation metamethods/handlers when converting to/from BSON documents.
  • Transparent conversion from Lua/JSON to BSON for convenience.
  • Automatic conversion of Lua numbers to/from BSON Int32, Int64 and Double types depending on their capacity without precision loss (when Lua allows it). Manual conversion is also available.

Dependencies

  • lua >= 5.1 (or luajit)
  • mongo-c-driver >= 1.16

Building and installing with LuaRocks

To build and install, run:

luarocks make

To install the latest release using luarocks.org, run:

luarocks install lua-mongo

Building and installing with CMake

To build and install, run:

cmake .
make
make install

To build for a specific Lua version, set USE_LUA_VERSION. For example:

cmake -D USE_LUA_VERSION=5.1 .

or for LuaJIT:

cmake -D USE_LUA_VERSION=jit .

To build in a separate directory, replace . with a path to the source.

To check your build, run:

make test

A local MongoDB server at mongodb://127.0.0.1 will be used for testing by default. Test settings can be configured in test/test.lua.

Getting started

Preparing the playground:

local mongo = require 'mongo'
local client = mongo.Client('mongodb://127.0.0.1')
local collection = client:getCollection('lua-mongo-test', 'test')
collection:drop() -- Clear collection

-- Common variables
local id = mongo.ObjectID()
local query1 = mongo.BSON('{ "age" : { "$gt" : 25 } }')
local query2 = mongo.BSON{_id = id}

Basic features and operations:

-- Implicit Lua/JSON to BSON conversion where BSON is required
collection:insert{_id = id, name = 'John Smith', age = 50}
collection:insert('{ "name" : "Bobby", "age" : 3 }')

-- Iterate documents in a for-loop
for person in collection:find({}, {sort = {age = -1}}):iterator() do
    print(person.name, person.age)
end

-- Fetch single document
local person = collection:findOne(query1):value()
print(person.name, person.age)

-- Access to BSON where needed
local bson = collection:findOne(query1)
print(bson) -- BSON is converted to JSON using tostring()

-- Explicit BSON to Lua conversion
local person = bson:value()
print(person.name, person.age)

-- Transparently include BSON documents in other documents
collection:update(query2, {age = 60, old = bson}) -- Update document
collection:remove(query2) -- Remove document

Bulk write operations can be used to execute multiple insert, update, replace and remove operations together. Executing write operations in batches reduces the number of network round trips increasing write throughput.

local bulk = collection:createBulkOperation()

-- Multiple insertions
bulk:insert{a = 1}
bulk:insert{b = 2}
bulk:insert{c = 3}

-- Multiple modifications
bulk:replaceOne({a = 1}, {b = 1})
bulk:updateMany('{}', '{ "$inc" : { "b" : 2 } }')
bulk:removeOne{c = 3}

-- Execute queued operations
bulk:execute()

The use of __toBSON metamethods and BSON handlers gives full control over how Lua values are represented in BSON documents and vice versa. In particular, this API facilitates support for Lua classes (tables with metatables) on their way to and/or from MongoDB.

local TestClass = {} -- Class metatable

local function TestObject(id, name) -- Constructor
    local object = {
        id = id,
        name = name,
    }
    return setmetatable(object, TestClass)
end

function TestClass:__tostring() -- Method
    return tostring(self.id) .. ' --> ' .. self.name
end

function TestClass:__toBSON() -- Called when object is serialized into BSON
    return {
        _id = self.id,
        binary = mongo.Binary(self.name), -- Store 'name' as BSON Binary for example
    }
end

-- A root '__toBSON' metamethod may return a table or BSON document.
-- A nested '__toBSON' metamethod may return a value, BSON type or BSON document.

-- BSON handler
local function handler(document)
    local id = document._id
    local name = document.binary:unpack() -- Restore 'name' from BSON Binary
    return TestObject(id, name)
end

-- Note that the same handler is called for each document. Thus, the handler should be able
-- to differentiate documents based on some internal criteria.

local object = TestObject(id, 'abc')
print(object)

-- Explicit BSON <-> Lua conversion
local bson = mongo.BSON(object)
local object = bson:value(handler)
print(object)

-- Store object
collection:insert(object)

-- Restore object
local object = collection:findOne(query2):value(handler)
print(object)

-- Iterate objects in a for-loop
for object in collection:find(query2):iterator(handler) do
    print(object)
end

Check out the API Reference for more information.

See also the MongoDB Manual for detailed information about MongoDB commands and CRUD operations.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].