Tag: mongoid

在Heroku上渲染大量JSON的有效方法

我用一个端点构建了一个简单的API。 它会抓取文件,目前有大约30,000条记录。 理想情况下,我希望能够通过一次http调用获取JSON中的所有记录。 这是我的Sinatra视图代码: require ‘sinatra’ require ‘json’ require ‘mongoid’ Mongoid.identity_map_enabled = false get ‘/’ do content_type :json Book.all end 我尝试过以下方法:使用multi_json require ‘./require.rb’ require ‘sinatra’ require ‘multi_json’ MultiJson.engine = :yajl Mongoid.identity_map_enabled = false get ‘/’ do content_type :json MultiJson.encode(Book.all) end 这种方法的问题是我得到错误R14(超出内存配额)。 当我尝试使用’oj’gem时,我得到了同样的错误。 我只是将一个很长的Redis字符串连接起来,但Heroku的redis服务是每月30美元,我需要的实例大小(> 10mb)。 我目前的解决方案是使用创建对象的后台任务,并在Mongoid对象大小限制(16mb)附近填充它们充满jsonified对象。 这种方法的问题:渲染仍然需要将近30秒,我必须在接收应用程序上运行后处理才能从对象中正确提取json。 有没有人有更好的想法如何在一次通话中为30k记录渲染json而无需切断Heroku?

可排序的表列在MongoDB中不起作用

我正在使用rails castvideohttp://railscasts.com/episodes/240-search-sort-paginate-with-ajax 。 它适用于Mysql数据库。 我想在Mongodb中实现这个。 我的application_helper.rb文件是这样的 def sortable(column, title = nil) title ||= column.titleize css_class = column == sort_column ? “current #{sort_direction}” : nil direction = column == sort_column && sort_direction == “asc” ? “desc” : “asc” link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class} end 我的products_controller.rb文件是这样的 helper_method :sort_column, […]

使用mongoid / mongodb提取,建模和更改数据模型

我正在寻找一种更好的方法,如果可能的话,使用惊人的mongoid ODM驱动程序来操纵现有的mondodb数据模型。 假设您有一个嵌入的一对多数据模型,如下所示: class User include Mongoid::Document field :nickname embeds_many :watchlists end class Watchlist include Mongoid::Document field :html_url field :description field :tags_array, type: Array embedded_in :user end 现在,对于所有用户,您只想提取每个监视列表的一部分,当且仅当它具有 tags_array == [“ruby”, “web”, “framework”] 只返回几个字段(不是整个监视列表文档): watchlist的html_url内容 关注列表的描述内容 和 相关父昵称(User.nickname) 我试过这样的事情: 1.9.2p290 :574 > Competitor = Struct.new(:html_url, :description, :user) => # 1.9.2p290 :575 > competitors = [] […]

为什么安装bson_ext会出错?

当我在Rails项目文件夹中执行以下命令时: gem install bson_ext 我收到这个错误: #result Building native extensions. This could take a while… ERROR: Error installing bson_ext: ERROR: Failed to build gem native extension. /home/absolute/.rvm/rubies/ruby-1.9.3-p0/bin/ruby extconf.rb checking for asprintf()… yes checking for ruby/st.h… yes checking for ruby/regex.h… yes checking for ruby/encoding.h… yes creating Makefile make compiling bson_buffer.c compiling cbson.c cbson.c: In function ‘write_element’: cbson.c:439:17: […]

Mongoid定制的setter / getters和super

我正在尝试修改属性Mongoid模型上的setter,但与ActiveRecord不同,我不能调用super来让Mongoid实际设置属性,因为模型使用include Mongoid::Document而不是ActiveRecord::Base的子类。 我希望能够做这样的事情。 class User include Mongoid::Document embeds_one :email_account def email_account=(_email_account) ret = super puts “email account updated!” do_something ret end end 除了,因为它不是一个子类,产生 NoMethodError: super: no superclass method 想法? 编辑: 你会怎么做一个吸气剂,比如 class User include Mongoid::Document embeds_one :email_address def email_address super || “myself@gmail.com” end end

如何在mongoid中使用或条件进行查询

如何在Mongoid中使用或条件进行查询。

Mongoid has_and_belongs_to_many关联

我试图让mongoid保存联想,但我只能让一方工作。 如果我有以下测试。 test “should add a user as a follower when a user follows the group” do @cali_group.followers = [] @user1.followed_groups << @cali_group assert_equal 1, @user1.followed_groups.count assert_equal 1, @cali_group.followers.count end 哪个失败了,因为@ cali_group.followers是[]。 我已经使用了一段时间,尝试了@cali_group.reload 。 但看起来在我的代码中执行此操作的唯一方法是使用连接的两端,即@cali_group.followers << @user1 。 如果必须的话,我可以在我的代码中这样做。 polco_group和用户的模型如下: https ://gist.github.com/1195048 完整的测试代码在这里: https : //gist.github.com/1195052

使用Mongoid,我可以“update_all”一次将值推送到多个条目的数组字段吗?

使用Mongoid,是否可以使用“update_all”将值推送到符合特定条件的所有条目的数组字段? 例: class Foo field :username field :bar, :type => Array def update_all_bars array_of_names = [‘foo’,’bar’,’baz’] Foo.any_in(username: foo).each do |f| f.push(:bar,’my_new_val’) end end end 我想知道是否有办法立即更新所有用户(将值’my_new_val’推送到每个匹配条目的“foo”字段)使用“update_all”(或类似的东西)而不是循环通过它们来更新他们一次一个。 我已经尝试了所有我能想到的东西,到目前为止还没有运气。 谢谢

嵌入式文档vs mongoid中的哈希数据类型

我找不到任何关于此的博客文章或文档。 它们,嵌入式文档和散列数据类型都非常相似。 每个人的利益或限制是什么? 考虑我的架构设计: class HistoryTracker include ::Mongoid::Document include ::Mongoid::Timestamps field :modifier, type: Hash, default: {} field :original, type: Hash, default: {} field :modified, type: Hash, default: {} field :changeset, type: Hash, default: {} end 我应该在这个HistoryTracker类中创建几个嵌入式文档吗? 或者只是用它? 索引怎么样?

限制聚合聚合中的聚合

我有这样的集合,但有更多的数据。 { _id: ObjectId(“db759d014f70743495ef1000”), tracked_item_origin: “winword”, tracked_item_type: “Software”, machine_user: “mmm.mmm”, organization_id: ObjectId(“a91864df4f7074b33b020000”), group_id: ObjectId(“20ea74df4f7074b33b520000”), tracked_item_id: ObjectId(“1a050df94f70748419140000”), tracked_item_name: “Word”, duration: 9540, } { _id: ObjectId(“2b769d014f70743495fa1000”), tracked_item_origin: “http://www.facebook.com”, tracked_item_type: “Site”, machine_user: “gabriel.mello”, organization_id: ObjectId(“a91864df4f7074b33b020000”), group_id: ObjectId(“3f6a64df4f7074b33b040000”), tracked_item_id: ObjectId(“6f3466df4f7074b33b080000”), tracked_item_name: “Facebook”, duration: 7920, } 我做一个聚合,ho返回分组数据,如下所示: {“_id”=>{“tracked_item_type”=>”Site”, “tracked_item_name”=>”Twitter”}, “duration”=>288540}, {“_id”=>{“tracked_item_type”=>”Site”, “tracked_item_name”=>”ANoticia”}, “duration”=>237300}, {“_id”=>{“tracked_item_type”=>”Site”, “tracked_item_name”=>”Facebook”}, “duration”=>203460}, {“_id”=>{“tracked_item_type”=>”Software”, “tracked_item_name”=>”Word”}, “duration”=>269760}, {“_id”=>{“tracked_item_type”=>”Software”, […]