Git Product home page Git Product logo

luamongo's Introduction

A Lua driver for MongoDB

Versions compatibility

LuaMongo mongo-c++-driver
v0.5 legacy-1.1
v0.4 legacy-0.9

LuaMongo v1.0 is expected when the modern c++ driver is marked as stable. See at the end of this file what changes are introduced from v0.4 to v0.5.

Compilation

The makefile automatically detects which platform and Lua version are you using, so for compilation you just need to do:

$ make

You can force the platform compilation by using $ make Linux or $ make Darwin. Additionally, you can force the Lua version by doing:

$ make LUAPKG=lua5.2

where lua5.2 can be replaced by lua5.1 and luajit.

Installation

Copy the library file mongo.so to any of the paths in LUA_CPATH environment variable or Lua string package.path.

Wiki Documentation

Support

Submit issues to the moai github site.

There is a Google Groups mailing list.

Example

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

How It Works

LuaMongo is a Lua library that wraps the MongoDB C++ API. Currently it has been tested with legacy-1.1.0 version of this driver. This mongo-cxx-driver release has been included at external folder, you can download it by executing:

$ git submodule update --init

The current implementation does not give you raw access to the BSON objects. BSON objects are passed to the API using a Lua table or a JSON string representation. Every returned BSON document is fully marshalled to a Lua table.

Installing

luarocks can be used to install LuaMongo last SCM version:

luarocks install "https://raw.githubusercontent.com/moai/luamongo/master/rockspec/luamongo-scm-0.rockspec"

or install any other of the versions available at rockspec directory.

For modern Linux systems, you will need to update your luarocks configuration file, usually located at /etc/luarocks/config.lua or /usr/local/etc/luarocks/config.lua, adding the following Lua table:

external_deps_dirs = {
  {
    prefix='/usr/',
    include='include',
    lib='lib',
  },
  {
    prefix='/usr/',
    include='include',
    lib='lib/i386-linux-gnu',
  },
  {
    prefix='/usr/',
    include='include',
    lib='lib/x86_64-linux-gnu',
  },
  {
    prefix='/usr/local',
  },
}

Changes from v0.4 to v0.5

  • GridFileBuilder has been introduced into legacy C++ driver, LuaMongo don't implements it any more. The constructor has been changed and now it only receives an instance of GridFS class.

  • db:ensure_index() function has been replaced by db:create_index(). The parameters of the new function are create_index(ns,keys,options) where keys and options can be Lua tables or JSON strings (both dictionaries which allow 'unique' and 'name' fields).

  • db:get_indexes() is now db:enumerate_indexes().

  • db:reset_index_cache() has been removed.

  • gridfs:find_file() requires a query. It has been added gridfs:find_file_by_name(filename) equivalent to old implementation.

luamongo's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

luamongo's Issues

Bad ObjectId's

require "mongo"

id = mongo.ObjectId("1234")
print( id[1] == "1234" ) -- true, should be false

Should give an error or return nil rather than accept a bad ObjectId.

add new release

Fix please your code in new release.
With work revision 4 my docker image don't work properly, but with current master all ok. I want make good Dockerfile and check only freezed code in version semantic. Thanks.

how to use query where ?

i use sort in query object it's work fine. but i need to to know how to add where?
sort = {}
sort['utime']=-1;
q1,err = mongo.Query.New()
ok,err = q1:sort(sort)

------ sone thing like that ----
twhere = {}
ok,err = q1:where(twhere);

Is there a "slaveok" option when connect to a replset

HI,

    I am using this driver to connect to a mongo replset(mongo version 2.0.2), it's impossible to retrieve data from a slave if we don't set "slaveok".  I checked the wiki page, but didn't find anything about this option.

Moving luamongo primary repository

Dear all contributors. This repository seems dead, and at least some contrbutors still working. At least, I am interested in continue with the develoment of this driver. I'm working in my own fork pakozm/luamongo, but I don't if it is possible to acquire the rights to own the main repo. Anyone has any ideas about this?

Undefined symbol when loading

Hello! Can't resolve this problem...
C++ driver 0.9.0

root@HO:/home/tg/luamongo2# lua -v
Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
root@HO:/home/tg/luamongo2# make
make[1]: Entering directory `/home/tg/luamongo2'
g++ -Wall -w -g -O2 -shared -fPIC -I/usr/include/mongo -I/usr/include/lua5.2   -I/usr/include/mongo-client -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include   main.o mongo_bsontypes.o mongo_dbclient.o mongo_replicaset.o mongo_connection.o mongo_cursor.o mongo_gridfile.o mongo_gridfs.o mongo_gridfschunk.o mongo_query.o utils.o mongo_cxx_extension.o mongo_gridfilebuilder.o -o mongo.so -llua5.2   -lmongoclient -lssl -lboost_thread -lboost_filesystem -lrt
make[1]: Leaving directory `/home/tg/luamongo2'
root@HO:/home/tg# lua test.lua
lua: error loading module 'mongo' from file './mongo.so':
    ./mongo.so: undefined symbol: _ZNK5mongo3OID8toStringEv
stack traceback:
    [C]: in ?
    [C]: in function 'require'
    test.lua:6: in main chunk
    [C]: in ?
root@HO:/home/tg# echo _ZNK5mongo3OID8toStringEv | c++filt 
mongo::OID::toString() const

auto_reconnect feature not working

Hi! Thank you for the great driver. Works flawlessly except auto_reconnect declared in the docs.
It is just keeping return "Connection error" on assert (or nil without). So I need to manually check for nil, reconnect and repeat the query.

About disconnect

Thanks for your lua binding so that I can play with Mongodb with Lua.

However, may I know how to do disconnect()?

I have searched a bit and does not see any related methods for disconnect().

LuaJIT

Hey guys

I'm not sure why Lua 5.2 is required for this project. It's basically one library which is currently forcing people to resign from performance benefits of LuaJIT and go for unnecessary Lua 5.2

Is there any real reason behind Lua 5.2?

629b687 seems to hang on db:connect

Hi,

Troubleshooting new install, seems to hang on connect
moai-luamongo-629b687


# uname -a
FreeBSD hunny.waitman.net 10.0-CURRENT FreeBSD 10.0-CURRENT #1: Mon Mar 26 19:48:52 PDT 2012
     [email protected]:/usr/obj/usr/src/sys/KEYSHIA  amd64

# ldd /usr/local/lib/lua/5.1/mongo.so 
/usr/local/lib/lua/5.1/mongo.so:
    libboost_thread.so => /usr/local/lib/libboost_thread.so (0x801343000)
    libboost_filesystem.so => /usr/local/lib/libboost_filesystem.so (0x80155b000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x801783000)
    libm.so.5 => /lib/libm.so.5 (0x801a93000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x801cb6000)
    libc.so.7 => /lib/libc.so.7 (0x80081b000)
    libthr.so.3 => /lib/libthr.so.3 (0x801ec3000)
    libboost_system.so => /usr/local/lib/libboost_system.so (0x8020e6000)

# mongo --version
MongoDB shell version: 2.0.4
# mongod --version
db version v2.0.4, pdfile version 4.5
Tue Apr 10 22:54:05 git version: nogitversion

# lua -v 
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio

# g++ -v
Using built-in specs.
Target: amd64-undermydesk-freebsd
Configured with: FreeBSD/amd64 system compiler
Thread model: posix
gcc version 4.2.1 20070831 patched [FreeBSD]


running a c++ test connects OK

ie,

#include <iostream>
#include "client/dbclient.h"

using namespace mongo;

void run() {
  DBClientConnection c;
  c.connect("localhost");
}

int main() {
  try {
    run();


g++ -g -O2 -fPIC testclient.cpp -I/usr/local/include/ 
    -I/usr/home/waitman/downloads/mongodb-src-r2.0.4/ 
    -L/usr/local/lib/ -lmongoclient -lboost_thread -lboost_filesystem

test client connects OK


# show log
Tue Apr 10 22:39:00 [initandlisten] connection accepted from 127.0.0.1:20154 #7
Tue Apr 10 22:39:00 [conn7] end connection 127.0.0.1:20154


but running the lua test hangs on connect (possibly forever but haven't waited that long). no errors/core etc 

require('mongo')
print ("1")
local db = assert(mongo.Connection.New())
print ("2")
assert(db:connect('127.0.0.1'))
print ("3")

never gets to 3. :(


Any help or suggestions much appreciated, thanks!

Waitman Gobble
San Jose California USA

How to specify readConcern for find()?

I am running a single mongodb.

However, I keep reading stale data from lua binding after nodejs complete the write operation. Therefore, I would like to specify read and write concern on both side to ensure no stale data.

in nodejs, I ensure the write concern by j and w.

    return this._db.collection('runtimeDataset').updateOne(
        {_id: cmd},
        updateContext,
        {j: true, w: 1}
    ).then(function () {
        return q.delay(1000);
    });

I have tried to wait for 10 seconds. It still reads the stale record.

But, I am not sure how to specify readConcern in this lua binding.

Or, are there any hints for not reading stale data?

Unexpected garbage collection

A problem exists when Lua garbage collector deletes a variable which has a reference in C++ but not in Lua. The following example shows a code which could end with a segmentation fault because of this issue:

function blah(db)
  local gridfs = mongo.GridFS.New(db, "foo")
  local file = gridfs:find_file("bar")
  return function()
    -- do whatever with file
    print(file:chunk(0):data())
  end
end

This code will produce arbitrary segmentation faults depending in the Lua garbage collection. The reference to gridfs only exists as local in the function blah. The returned closure captures the local file but not gridfs. So, at the end Lua garbage collector will delete gridfs variable because it is not referenced. Unfortunatelly, the GridFile C++ object contains a reference to this variable, but Lua is not acknowledged of that, and the call to file:chunk(0) will produce a segmentation fault.

Not able to install luamongo in windows 7

C:\Users\s.panigrahi.ext>luarocks install "https://raw.githubusercontent.com/moai/luamongo/master/rockspec/luamongo-scm-0.rockspec

Error: LuaRocks 2.0.2 bug (please report at [email protected]).
...\Program Files (x86)\Lua\5.1\lua/luarocks\fs\lua.lua:526: attempt to concatenate local 'err' (a nil value)
stack traceback:
...\Program Files (x86)\Lua\5.1\lua/luarocks\fs\lua.lua:526: in function 'download'
...:\Program Files (x86)\Lua\5.1\lua/luarocks\fetch.lua:33: in function 'fetch_url'
...:\Program Files (x86)\Lua\5.1\lua/luarocks\fetch.lua:70: in function 'fetch_url_at_temp_dir'
...:\Program Files (x86)\Lua\5.1\lua/luarocks\fetch.lua:231: in function 'load_rockspec'
...:\Program Files (x86)\Lua\5.1\lua/luarocks\build.lua:101: in function <...:\Program Files (x86)\Lua\5.1\lua/luarocks\build.lua:97>
(tail call): ?
(tail call): ?
(tail call): ?
[C]: in function 'xpcall'
...am Files (x86)\Lua\5.1\lua\luarocks\command_line.lua:143: in function 'run_command'
C:\Program Files (x86)\Lua\5.1\luarocks.lua:20: in main chunk
[C]: ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.