Lua Operation MongoDB DB instance

Source: Internet
Author: User
Tags auth findone lua mongodb require socket

A recent job is to use Nginx + LUA to implement an API to manipulate the MongoDB database, mainly implementing its count and query functions. Did not write Lua before, so also reluctantly to get started, on the basis of the CLOUDWU Lua-mongo implementation of the MongoDB API operation.

Cloudwu's Lua-mongo driver realizes the function of connecting MONGO, doing basic operations such as find and findone, so the method of count and query is added on the basis of Lua-mongo. The specific contents of the amendments are as follows:

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

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

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

After the revised code see: Lua-mongo

The LUA code for the specific operation MongoDB is as follows:
Copy the 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
--Constants
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}
--time-related fields that need to be converted using Bson
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) does
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 _, the object in pairs (results) does
For key, value in pairs (object) does
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) does
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)

Source: http://www.jb51.net/article/62871.htm

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.