Lua Operations MongoDB Database instance _lua

Source: Internet
Author: User
Tags auth findone lua mongodb

One recent job is to implement an API to operate a MONGODB database using Nginx + LUA, primarily to implement its count and query functions. has not written Lua before, so also reluctantly to start, in the Cloudwu Lua-mongo based on the implementation of the operation of the MongoDB API.

Cloudwu's Lua-mongo Drive realizes the connection MONGO, carries on the basic operation and so on find and findone the function, therefore has added the count and the query and so on the Lua-mongo Foundation method. The specific contents of the amendment are as follows:

1, API based on luajit-2.0 development, equivalent to LUA 5.1, need to use lua-compat-5.2 compatible LUA 5.2

2. Replace Mongo.socket module with NGX.SOCKET.TCP

3, increased the Count,query,auth and other methods

After the modified code, see: Lua-mongo

The specific operation MongoDB's LUA code is as follows:

Copy Code code as follows:

--Lua MONGO test script
--Utils
function String:split (Sep)
Local sep, fields = Sep or ":", {}
Local pattern = String.Format ("([^%s]+)", Sep)
Self:gsub (pattern, function (c) fields[#fields + 1] = C-end)
return fields
End
--constant
HOST = "127.0.0.1"
PORT = 27017
Keepalive_timeout = 60000
keepalive_size = 100
Conn_timeout = 3000
Db_user = "USER"
DB_PASSWD = "Password"
db_name = "blog"
Db_collection = "article"
--Reference
MONGO = require ("MONGO")
Cjson = require ("Cjson.safe")
Cbson = require ("Bson")
--State
Local status_msg = "Error"
Local Status_code = 500
Local message = "Unknown error"
Local mongo_query = {["category_id"] = {["$in"] = {1,2,3,4}}, ["status"] = {["$ne"] = 2}, ["create_time"] = {["$lte"] = 14 27102260}}
Local Mongo_sort = {["create_time"] = 1}
Local Mongo_limit = 100
Local Mongo_skip = 0
Local mongo_fields = {[' _id '] = false}
-involves the time field, needs to use the Bson to transform
If mongo_query["Create_time"] Then
Local create_time = mongo_query["Create_time"]
Local T = Type (create_time)
If T = = "TABLE" Then
For key, value in pairs (create_time) do
mongo_query["Create_time"][key] = cbson.date (value)
End
Else
mongo_query["create_time"] = cbson.date (create_time)
End
End
Local conn = Mongo.client ({host = host, Port = port})
Conn:set_timeout (Conn_timeout)
Local db = Conn:getdb (db_name)
Local reused_times = Conn:get_reused_times ()
If Reused_times = 0 Then
Db:auth (Db_user, DB_PASSWD)
End
Local col = db:getcollection (db_collection)
Local result = {}
--Count
Local count, err = Col:count (mongo_query)
Local OK, err = conn:set_keepalive (keepalive_timeout, Keepalive_size)
If Count ~= Nil then
result = Count
Status_code = 200
status_msg = "OK"
Message = "Success"
End
--Query
Local Bson_obj
If Mongo_sort Then
Bson_obj = Cbson.encode_order ("$query", Mongo_query, "$orderby", Mongo_sort)
Else
Bson_obj = Cbson.encode ({["$query"] = mongo_query})
End
Local results = Col:query (Bson_obj, Mongo_fields, Mongo_skip, Mongo_limit)
Local OK, err = conn:set_keepalive (keepalive_timeout, Keepalive_size)
If results then
For _, object in pairs (results) do
For key, value in pairs (object) do
if value = = Cbson.null Then
Object[key] = Cjson.null
Else
Local type_name, value = Cbson.type (value)
Object[key] = value
End
End
End
result = Results
Status_code = 200
status_msg = "OK"
Message = "Success"
End
--FindOne
Local results = Col:findone ({["id"] = 14})
Local OK, err = conn:set_keepalive (keepalive_timeout, Keepalive_size)
If results then
For key, value in pairs (results) do
if value = = Cbson.null Then
Results[key] = Cjson.null
Else
Local type_name, value = Cbson.type (value)
Results[key] = value
End
End
result = Results
Status_code = 200
status_msg = "OK"
Message = "Success"
End
Ngx.status = Status_code
Json_out = Cjson.encode ({status = status_msg, message = message, data = result})
ngx.header["content-length"] = Json_out:len ()
Ngx.print (Json_out)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.