麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > 綜合 > 正文

Lua 操作 MongoDB 數據庫實例

2024-07-21 23:04:25
字體:
來源:轉載
供稿:網友

最近有個工作是使用Nginx + Lua實現一個操作MongoDB數據庫的API,主要實現其count和query功能。之前沒有寫過Lua,于是也就勉強著上手,在cloudwu的 lua-mongo 的基礎上實現了操作MongoDB的API。

cloudwu的lua-mongo驅動實現了連接Mongo,進行find和findOne等基本操作的功能,所以在lua-mongo的基礎上增加了count和query等方法。修改的具體內容如下:

1、API基于luajit-2.0開發,相當于lua 5.1,需要使用lua-compat-5.2兼容lua 5.2

2、使用ngx.socket.tcp替換mongo.socket模塊

3、增加了count,query,auth等方法

修改之后的代碼見: lua-mongo

具體的操作MongoDB的lua代碼如下:

復制代碼 代碼如下:

-- 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
-- 常量
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"
-- 引用
mongo = require("mongo")
cjson = require("cjson.safe")
cbson = require("bson")
-- 狀態
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"] = 1427102260}}
local mongo_sort = {["create_time"] = 1}
local mongo_limit = 100
local mongo_skip = 0
local mongo_fields = { ["_id"] = false }
-- 涉及到時間的字段,需要使用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) 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)

 

 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 红杏亚洲影院一区二区三区 | 久久精品久久精品国产大片 | 欧美性受xxxxxx黑人xyx性爽 | 亚洲日韩中文字幕一区 | 成人在线免费视频播放 | h色网站免费观看 | 中文字幕一区二区三区四区 | 午夜免费一区 | 福利免费视频 | 午夜视频福利 | 国产艳妇av视国产精选av一区 | 国产91丝袜在线播放 | 黄色免费在线视频网站 | 特一级黄色毛片 | 国产一级免费电影 | aaaaaaa毛片| av免费在线观看国产 | 久久久久久久久久综合 | 日本最新免费二区三区 | 国产成人高清成人av片在线看 | 久久久久久久久免费 | 成人男女视频 | 色播视频在线播放 | 欧美 日韩 亚洲 中文 | 国产亚洲精品久久久久久久久久 | 日本网站一区 | 国产成人高清在线观看 | 久久免费视频一区二区三区 | 欧美国产第一页 | 精国品产一区二区三区有限公司 | 中文字幕国产日韩 | 日韩视频1| 欧美精品免费一区二区三区 | 十级毛片 | 黄色网页在线看 | 成人av一区二区免费播放 | 成人性视频在线 | 国产精品久久久久久久久久久久午夜 | 国产理论视频在线观看 | 黄色高清免费网站 | 亚洲啪 |