2015-11-01 58 views
0

我有愚蠢的,简单的一块红宝石以下/蒙戈代码中的一个问题:蒙戈/ Ruby的未定义的方法'更新” ::收藏

require 'mongo' 

client = Mongo::Client.new(['127.0.0.1:27017'], database: 'dbs') 
items = client[:items].find('issues.category': 'general') 

items.each do |item| 
    item2 = item 
    client[:items].update({ '_id': item['_id'] } , item2) 
end 

我得到undefined method "update" for #<Mongo::Collection:0x4544580 namespace=dbs.items> (NoMethodError)

回答

1

没有update MongoDB rub​​y​​驱动程序的方法,有update_oneupdate_many

在你的情况下,它看起来像你想更新所有:

client[:items].update_many({ :id => item['_id'] }, item2) 

见文档here

+0

你是100%正确的,工作的,谢谢。 但在这 - https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial#updating-documents-with-update教程我用那里只有'更新'方法,这使我困惑了 – Vla

+0

这是混乱! – Anthony