最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Lua 操作 MongoDB 數(shù)據(jù)庫(kù)實(shí)例

 更新時(shí)間:2015年03月26日 10:02:27   投稿:junjie  
這篇文章主要介紹了Lua 操作 MongoDB 數(shù)據(jù)庫(kù)實(shí)例,本文給出了修改后的lua-mongo API和具體的操作MongoDB 數(shù)據(jù)庫(kù)代碼,需要的朋友可以參考下

最近有個(gè)工作是使用Nginx + Lua實(shí)現(xiàn)一個(gè)操作MongoDB數(shù)據(jù)庫(kù)的API,主要實(shí)現(xiàn)其count和query功能。之前沒(méi)有寫(xiě)過(guò)Lua,于是也就勉強(qiáng)著上手,在cloudwu的 lua-mongo 的基礎(chǔ)上實(shí)現(xiàn)了操作MongoDB的API。

cloudwu的lua-mongo驅(qū)動(dòng)實(shí)現(xiàn)了連接Mongo,進(jìn)行find和findOne等基本操作的功能,所以在lua-mongo的基礎(chǔ)上增加了count和query等方法。修改的具體內(nèi)容如下:

1、API基于luajit-2.0開(kāi)發(fā),相當(dāng)于lua 5.1,需要使用lua-compat-5.2兼容lua 5.2

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

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

修改之后的代碼見(jiàn): lua-mongo

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

復(fù)制代碼 代碼如下:

-- 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")
-- 狀態(tài)
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 }
-- 涉及到時(shí)間的字段,需要使用bson轉(zhuǎn)化一下
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)

相關(guān)文章

  • Lua中的table淺析

    Lua中的table淺析

    這篇文章主要介紹了Lua中的table淺析,本文講解了table的構(gòu)造方法、內(nèi)置函數(shù)unpack等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • 詳解Lua中的while循環(huán)語(yǔ)句的使用

    詳解Lua中的while循環(huán)語(yǔ)句的使用

    這篇文章主要介紹了詳解Lua中的while循環(huán)語(yǔ)句的使用,是Lua入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Lua實(shí)現(xiàn)類(lèi)繼承

    Lua實(shí)現(xiàn)類(lèi)繼承

    這里給大家演示的是一個(gè)使用lua實(shí)現(xiàn)類(lèi)繼承的示例,實(shí)現(xiàn)類(lèi)繼承其實(shí)有很多種寫(xiě)法,這里是本人常用的方法,推薦給大家。
    2015-03-03
  • Lua的編譯、執(zhí)行和調(diào)試技術(shù)介紹

    Lua的編譯、執(zhí)行和調(diào)試技術(shù)介紹

    這篇文章主要介紹了Lua的編譯、執(zhí)行和調(diào)試技術(shù)介紹,本文著重講解了對(duì)錯(cuò)誤的處理,另外也講解了編譯和執(zhí)行等知識(shí),需要的朋友可以參考下
    2015-04-04
  • 簡(jiǎn)單談?wù)刲ua和c的交互

    簡(jiǎn)單談?wù)刲ua和c的交互

    要理解Lua和C++交互,首先要理解Lua堆棧。簡(jiǎn)單來(lái)說(shuō),Lua和C/C++語(yǔ)言通信的主要方法是一個(gè)無(wú)處不在的虛擬棧。棧的特點(diǎn)是先進(jìn)后出。
    2016-01-01
  • Lua中獲取utf8字符串長(zhǎng)度的方法和自定義函數(shù)

    Lua中獲取utf8字符串長(zhǎng)度的方法和自定義函數(shù)

    這篇文章主要介紹了Lua中獲取utf8字符串長(zhǎng)度的方法和自定義函數(shù),本文給出了代碼實(shí)例并講解了UTF8的編碼規(guī)則,需要的朋友可以參考下
    2015-04-04
  • 編寫(xiě)高性能Lua代碼的方法

    編寫(xiě)高性能Lua代碼的方法

    這篇文章主要介紹了編寫(xiě)高性能Lua代碼的方法,本文是基于Lua語(yǔ)言的創(chuàng)造者Roberto Ierusalimschy的一篇文章翻譯改寫(xiě)而來(lái),需要的朋友可以參考下
    2014-10-10
  • Lua中table庫(kù)函數(shù)方法介紹

    Lua中table庫(kù)函數(shù)方法介紹

    這篇文章主要介紹了Lua中table庫(kù)函數(shù)方法介紹,本文講解了concat、insert、maxn、remove、sort、foreachi等方法,需要的朋友可以參考下
    2014-11-11
  • Lua獲取utf8字符串長(zhǎng)度和字符串截取并用...代替

    Lua獲取utf8字符串長(zhǎng)度和字符串截取并用...代替

    這篇文章主要介紹了Lua獲取utf8字符串長(zhǎng)度和字符串截取并用...代替,本文直接給出兩個(gè)函數(shù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04
  • Lua中__index和__newindex之間的沉默與合作

    Lua中__index和__newindex之間的沉默與合作

    這篇文章主要介紹了Lua中__index和__newindex之間的沉默與合作,本文著重講解了__index和__newindex之間的聯(lián)系,需要的朋友可以參考下
    2014-09-09

最新評(píng)論

湖南省| 荔浦县| 朝阳区| 纳雍县| 杭州市| 绍兴县| 山丹县| 拉萨市| 保靖县| 察雅县| 沽源县| 台中市| 喀什市| 大城县| 城固县| 随州市| 舞钢市| 绥滨县| 建平县| 伊吾县| 三台县| 宜宾县| 清远市| 贺兰县| 四会市| 义乌市| 汕尾市| 通州区| 全南县| 明水县| 汕尾市| 宝鸡市| 临沂市| 扎赉特旗| 平武县| 元朗区| 辽阳县| 光泽县| 高雄市| 太白县| 开阳县|