将属性添加到couchdb

时间:2011-11-18 11:31:35

标签: ruby-on-rails-3 couchdb couchrest

我已使用couchrest设置了couchdb数据库,以便与ruby on rails应用程序一起使用。

现在数据库有大约100K的记录,我想添加一个字段来存储整数格式的时间戳 尝试在循环中更新它会导致请求超时。

将新字段/属性添加到couchdb的最佳方法是什么?

代码

pages = Page.all
pages.each do |p|
  p.update_attributes(:created_at_to_i => p.created_at.to_i)
  puts p.created_at_to_i.inspect
end

跟踪

>> Page.update_timestamp
RestClient::RequestTimeout: Request Timeout
    from /usr/local/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:184:in `transmit'
    from /usr/local/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
    from /usr/local/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
    from /usr/local/lib/ruby/gems/1.8/gems/couchrest-1.1.2/lib/couchrest/rest_api.rb:89:in `execute'
    from /usr/local/lib/ruby/gems/1.8/gems/couchrest-1.1.2/lib/couchrest/rest_api.rb:45:in `get'
    from /usr/local/lib/ruby/gems/1.8/gems/couchrest-1.1.2/lib/couchrest/database.rb:260:in `view'
    from /usr/local/lib/ruby/gems/1.8/gems/couchrest-1.1.2/lib/couchrest/design.rb:52:in `view_on'
    from /usr/local/lib/ruby/gems/1.8/gems/couchrest_model-1.1.2/lib/couchrest/model/views.rb:142:in `fetch_view'
    from /usr/local/lib/ruby/gems/1.8/gems/couchrest_model-1.1.2/lib/couchrest/model/views.rb:135:in `fetch_view_with_docs'
    from /usr/local/lib/ruby/gems/1.8/gems/couchrest_model-1.1.2/lib/couchrest/model/views.rb:104:in `view'
    from /usr/local/lib/ruby/gems/1.8/gems/couchrest_model-1.1.2/lib/couchrest/model/document_queries.rb:12:in `all'
    from /var/www/monitoring_couch/app/models/page.rb:309:in `update_timestamp'
    from (irb):2

1 个答案:

答案 0 :(得分:0)

要添加新属性,您需要加载每个文档,修改它并将其保存回数据库。你可以使用bulk update api来做到这一点,但对于只有10万,我认为这不值得额外的麻烦。

请发布您的更新脚本以及发生超时时获得的回溯。