我想每小时更新一份大量的文件.
这是相当简单的模型:
class Article
include Mongoid::Document
field :article_nr, :type => Integer
field :vendor_nr, :type => Integer
field :description, :type => String
field :ean
field :stock
field :ordered
field :eta
Run Code Online (Sandbox Code Playgroud)
所以每小时我都会得到一个新的库存清单,其中:stock,:ordered和:eta"可能"已经改变,我需要更新它们.
编辑:stocklist只包含
:article_nr, :stock, :ordered, :eta
Run Code Online (Sandbox Code Playgroud)
我解析哈希
在SQL中我会采取路由到国外将article_nr键入"stock"表,删除整个stock表,并运行"collection.insert"或类似的东西
但这种方法似乎不适用于mongoid.任何提示?我无法理解collection.update和更改belongs_to和has_one上的外键似乎不起作用(尝试过,但是然后Article.first.stock为零)
但是必须有一种更快的方法,而不是迭代库存列表的哈希数组并做类似的事情
Article.where( :article_nr => stocklist['article_nr']).update( stock: stocklist['stock'], eta: stocklist['eta'],orderd: stocklist['ordered'])
Run Code Online (Sandbox Code Playgroud)