在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):neoxic/lua-mongo开源软件地址(OpenSource Url):https://github.com/neoxic/lua-mongo开源编程语言(OpenSource Language):C 79.5%开源软件介绍(OpenSource Introduction):MongoDB Driver for Lualua-mongo is a binding to MongoDB C Driver 1.16 or higher for Lua:
Dependencies
Building and installing with LuaRocksTo build and install, run:
To install the latest release using luarocks.org, run:
Building and installing with CMakeTo build and install, run:
To build for a specific Lua version, set
or for LuaJIT:
To build in a separate directory, replace To check your build, run:
A local MongoDB server at Getting startedPreparing 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 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. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论