好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Lua 操作 MongoDB 數據庫實例

Lua 操作 MongoDB 數據庫實例

熱門標簽:漳州智云呼電話機器人 新岸線智能電銷機器人 武漢外呼防封系統多少錢 個人怎么在地圖標注需要的店鋪 百度地圖標注早餐區域 冀州市地圖標注 怎么去除地圖標注 地圖標注大廈 清朝地圖標注哈爾濱

最近有個工作是使用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)

標簽:濰坊 天門 臺灣 儋州 金昌 天門 德宏 宣城

巨人網絡通訊聲明:本文標題《Lua 操作 MongoDB 數據庫實例》,本文關鍵詞  Lua,操作,MongoDB,數據庫,實例,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Lua 操作 MongoDB 數據庫實例》相關的同類信息!
  • 本頁收集關于Lua 操作 MongoDB 數據庫實例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 国产91?国语对白在线| 特级中国aaa毛片| 宝贝直播在线观看高清视频免费| 小柔婬荡高潮呻吟无码视频| 色欲91AⅤ蜜臀aV一区二区| 美腿网| 一女多男肉肉| 色戒在线观看视频| 澡堂洗澡吃老头亅j| 久久伊人蜜桃av一区二区| 亚洲精品成人网| 翁熄浪| 真实国产乱子伦清晰对白视频| 国内精品久久久久影院亚洲| 公啊灬啊灬啊灬快灬深用力视频 | 免费人成黄页网站在线观看国产| 男人扒女人??捏??视频国外| 免费一级特黄3大片视频| 三级国产三级国语三级| 99精品国产美女福到在线不卡| 天天碰天天摸天天爽| 老师穿黑色旗袍用白丝夹到爽| 国产老熟妇乱XXXXX小仙踪林 | k8经典片高清在线观看| 日本少妇做爰全过程毛片叶玉卿| 国产精品初高中女破苞| 国产精品va在线观看无| 农村打工妇女毛片| 国产成人免费高清激情视频| 欧美另类69| 精品国产鲁一鲁一区二区赤裸特工| 天天做天天做天天爱| 放荡的裸模h文小说| 男生和女生上免费看| 秘书婬荡呻吟揉捏丰满奶头电影| 欧美一级a视频免费放| 高h榨乳疏通乳孔扩张| 日本洗澡偷窥在线观看| 八戒八戒资源在线高清| 中国女人牲色生活片| 一级性生活免费看|