我自个回复吧,经过不断地尝试,总算得到结果了,就是需要重新构造t_old和t_new这两个lua表,然后采用update语句就可以了。其中,t_old和t_new只有一层大括号.在 2016年10月29日星期六 UTC+8下午12:36:43,maxwa...@gmail.com写道:
请教大家一个关于mongodb,update问题,谢谢大家。
n, err = col:update(selector, update, upsert, multiupdate, safe)
Returns number of rows been updated or nil for error.
- upsert, if set to
1
, the database will insert the supplied object into the collection if no matching document is found, default to 0
. - multiupdate, if set to
1
, the database will update all matching objects in the collection. Otherwise only updates first matching doc, default to 0
. Multi update only works with $ operators. - safe can be a boolean or integer, defaults to
0
. If 1
, the program will issue a cmd getlasterror
to server to query the result. If false
, return value n
would always be -1
上面是lua-resty-mongol官方的api文档,但是我测试的时候总感觉不对。
local t = col:find_one({name = "shen"})
local t_new = {{
userName = "gai",
age = 10,
createTime = ngx.time(),
_class = "com.whty.assis.api.model.User"
}}
ngx.say("-------update------------")
local n, err = col:update(t, t_new, 0, 0, true)
if n then
ngx.say("update successed: " .. n)
else
ngx.say("update failed")
end
----------------------
然后查看mongo的数据时,结果如下:
![]()