mongodbx.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. local mongodbx = {}
  2. local skynet = require "skynet"
  3. local MONGODBL_POOL
  4. -- 有些 服务 不允许在 init 阶段处理
  5. -- 暂时需改成 第一次调用 查询
  6. -- skynet.init(function ()
  7. -- MONGODBL_POOL = skynet.queryservice("mongodbpool")
  8. -- end)
  9. --show dbs;
  10. -- use ant_account
  11. --db.dropDatabase()
  12. -- show collections
  13. -- db.collection.find() --collection需替换对应具体名字
  14. --db.collection.find( { qty: { $gt: 4 } } )
  15. local function block_query()
  16. -- body
  17. if not MONGODBL_POOL then
  18. MONGODBL_POOL = skynet.queryservice("mongodbpool")
  19. end
  20. end
  21. function mongodbx.find(table_name, cname, conds)
  22. block_query()
  23. return skynet.call(MONGODBL_POOL, "lua", "find", table_name, cname, conds)
  24. end
  25. function mongodbx.findOne(table_name, cname, conds)
  26. block_query()
  27. return skynet.call(MONGODBL_POOL, "lua", "findOne", table_name, cname, conds)
  28. end
  29. function mongodbx.aggregate(table_name, cname, operation)
  30. block_query()
  31. return skynet.call(MONGODBL_POOL, "lua", "aggregate", table_name, cname, operation)
  32. end
  33. function mongodbx.upsert(table_name, cname, conds, datas)
  34. block_query()
  35. return skynet.call(MONGODBL_POOL, "lua", "upsert", table_name, cname, conds, datas)
  36. end
  37. function mongodbx.updateMany(table_name, cname, conds, datas)
  38. block_query()
  39. return skynet.call(MONGODBL_POOL, "lua", "updateMany", table_name, cname, conds, datas)
  40. end
  41. function mongodbx.insert(table_name, cname, datas)
  42. block_query()
  43. return skynet.call(MONGODBL_POOL, "lua", "insert", table_name, cname, datas)
  44. end
  45. function mongodbx.batch_insert(table_name, cname, datas)
  46. block_query()
  47. return skynet.call(MONGODBL_POOL, "lua", "batch_insert", table_name, cname, datas)
  48. end
  49. function mongodbx.del(table_name, cname, conds)
  50. block_query()
  51. return skynet.call(MONGODBL_POOL, "lua", "del", table_name, cname, conds)
  52. end
  53. function mongodbx.exec(cmd, ...)
  54. block_query()
  55. return skynet.call(MONGODBL_POOL, "lua", cmd, ...)
  56. end
  57. return mongodbx