bigplum / lua-resty-mongol

A ngx_lua driver for mongodb -- Deprecated for not updating with the mongodb version
198 stars 67 forks source link

获取表内所有数据时返回空 #24

Closed linuxlan closed 10 years ago

linuxlan commented 10 years ago

使用 cursor = col:find(query, returnfields, num_each_query) 进行查询数据库的时候返回空

local cursor = table::find()

这样使用不行么?

linuxlan commented 10 years ago

我怎么获取一个表里面的所有数据呢?难道必须要指定一个条件才行?

bigplum commented 10 years ago

r = col:find({})

linuxlan commented 10 years ago

15 local user_table = mongo_db:get_col("events") 16
17 local cursor = user_table:find({})

尝试过,这样不行

bigplum commented 10 years ago

这个测试没问题啊

         ok, err = conn:connect("127.0.0.1")
         if not ok then
             ngx.say("connect failed: "..err)
         end

         local db = conn:new_db_handle("test")

         col = db:get_col("test")
         col:delete({name="puppy"})

         for i = 1, 10 do
             col:insert({{name="puppy", n=i, m="foo"}})
             col:insert({{name="miao", n=i, m="blaaaa"}})
         end

         r = col:find({})
         for i , v in r:pairs() do
             ngx.say(v["n"])
             ngx.say(v["name"])
         end
linuxlan commented 10 years ago

db.events.find() { "_id" : ObjectId("52a834204d1a4ebca05220b5"), "content" : "不好", "send_type" : "text", "create_time" : 1386755104000, "uid" : "jWn42zCGHq6qw8MdFSMgulv8ol4=", "create" : 1, "event_title" : "您好" } { "_id" : ObjectId("52a839ff72bfb1aa82a3686a"), "content" : "haha", "send_type" : "text", "create_time" : 1386756607000, "uid" : "jWn42zCGHq6qw8MdFSMgulv8ol4=", "create" : 1, "event_title" : "真的不好" } 我查询了,表中有数据。

14 local p = "/www/wdlinux/nginx-1.2.9/lib/model/" 15 local m_package_path = package.path 16 package.path = string.format("%s?.lua;%s?/init.lua;%s", p, p, m_package_path) 17 18 19 local mongol = require "resty.mongol" 20 local conn = mongol:new() 21 local ok, err = conn:connect("127.0.0.1") 22 local mongo_db = conn:new_db_handle ( "weixin" ) 23 24 local weixin_table = mongo_db:get_col("events") 25 local cursor = weixin_table:find({})

linuxlan commented 10 years ago

但是我这真的出不来。为什么呢?好神奇啊。。

linuxlan commented 10 years ago

我在使用其他API ,比如:insert和 find_one 的时候都没有问题。只有在调用find的时候出现这个问题

linuxlan commented 10 years ago

15 local user_table = mongo_db:get_col("event")
16
17 local cursor = user_table:find({})

On 2013年12月11日, at 下午5:37, Simon notifications@github.com wrote:

r = col:find({})

— Reply to this email directly or view it on GitHub.

linuxlan commented 10 years ago

您好: 我在用_id 做find_one的时候 传入 _id的String 导致查询失败,请问如何把_id的string转化成对应的Object呢?

        谢谢

On 2013年12月11日, at 下午6:26, Simon notifications@github.com wrote:

这个测试没问题啊

     ok, err = conn:connect("127.0.0.1")
     if not ok then
         ngx.say("connect failed: "..err)
     end

     local db = conn:new_db_handle("test")

     col = db:get_col("test")
     col:delete({name="puppy"})

     for i = 1, 10 do
         col:insert({{name="puppy", n=i, m="foo"}})
         col:insert({{name="miao", n=i, m="blaaaa"}})
     end

     r = col:find({})
     for i , v in r:pairs() do
         ngx.say(v["n"])
         ngx.say(v["name"])
     end

— Reply to this email directly or view it on GitHub.