使用Rails指南中的这个修改示例,如何使用mongoid建模关系"has_many:through"关联?
挑战是mongoid不支持has_many:通过ActiveRecord.
# doctor checking out patient
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
has_many :meeting_notes, :through => :appointments
end
# notes taken during the appointment
class MeetingNote < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
has_many :physicians, :through => :appointments
end
# the patient
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
has_many :meeting_notes, :through => :appointments
end
# the appointment
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient …
Run Code Online (Sandbox Code Playgroud) 我已经尝试了MongoMapper,它功能齐全(提供几乎所有AR功能),但我对使用大型数据集时的性能不是很满意.有没有人比较Mongoid?任何性能提升?
我修改了一个模型,因此它包含一个新字段,例如......
field :url, :type => String
我使用activeadmin,所以当我创建一个新条目@model.url
为空时,在更改模式之前创建的条目中它是零.我如何选择两者?我试过了:
# Returns nils and strings
Model.where(:url.ne => "").count
# Returns strings and ""
Model.where(:url.ne => nil).count
# Returns strings, nils and ""
Model.where(:url.ne => ["", nil]).count
Run Code Online (Sandbox Code Playgroud)
或者,如果有这种情况的最佳做法,请告诉我.
我通过Mongoid集成使用MongoDB,以及在项目中使用ActiveRecord.我想为活动记录生成迁移,而Mongoid是我运行时的默认值.
rails g migration
Run Code Online (Sandbox Code Playgroud)
有关如何将AR指定为迁移,模型等的默认生成器的任何想法?
谢谢!
使用MongoDB文档中的字段ID或_ID有什么区别吗?
我问这个,因为我通常使用"_id",但是我在文档中看到了这种类型({id:-1}):http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs# OptimizingObjectIDs-Sortbyidtosortbyinsertiontime
编辑
事实证明文件是错误的.
我有这个小测试脚本:
require 'mongo'
mongo_client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test')
mongo_client[:collection].insert_one({a: 1})
Run Code Online (Sandbox Code Playgroud)
这是控制台输出:
$ ruby test.rb
D, [2015-05-17T21:12:05.504986 #25257] DEBUG -- : MONGODB | Adding 127.0.0.1:27017 to the cluster. | runtime: 0.0212ms
D, [2015-05-17T21:12:05.531238 #25257] DEBUG -- : MONGODB | COMMAND | namespace=admin.$cmd selector={:ismaster=>1} flags=[] limit=-1 skip=0 project=nil | runtime: 24.5481ms
D, [2015-05-17T21:12:05.554532 #25257] DEBUG -- : MONGODB | COMMAND | namespace=test.$cmd selector={:insert=>"collection", :documents=>[{:a=>1, :_id=><BSON::ObjectId:0x21935660 data=5558e80553657262a9000000>}], :writeConcern=>{:w=>1}, :ordered=>true} flags=[] limit=-1 skip=0 project=nil | runtime: 21.1718ms
Run Code Online (Sandbox Code Playgroud)
我想禁用那些日志消息,我不想要一个脏的STDOUT.我没有在ruby驱动程序中找到任何选项,而且我也尝试/etc/mongod.conf
使用这些指令进行编辑(但它没有修复它):
verbose = false …
Run Code Online (Sandbox Code Playgroud) 这必须要求很多,但记录很差.http://mongoid.org/en/mongoid/docs/querying.html没有提及
我正在尝试检查用户是否存在(下面是AND查询),如何将其更改为OR
类型查询
Username.where(:username=>@username, :email=>@email)
Run Code Online (Sandbox Code Playgroud)
(电子邮件或用户名必须匹配).
我在网上发现了一些非常复杂的方法,包括发送直接的javascript(来自):http://omarqureshi.net/articles/2010-6-17-using-or-in-mongoid
当然必须有一个简单明确的语法来正确地做到这一点?
我用谷歌搜索了所有其他人,但我找不到答案.问题是:
嗨,我怎么能用Mongoid批量插入到MongoDB?
使用rails 3和mongoDB与mongoid适配器,如何批量查找到mongo DB?我需要获取特定mongo数据库集合中的所有记录,并在solr中索引它们(搜索数据的初始索引).
我遇到的问题是,做Model.all会抓取所有记录并将它们存储到内存中.然后当我处理它们并在solr中索引时,我的内存被吃掉了,进程就死了.
我要做的是在mongo中批量查找,这样我就可以一次迭代超过1,000条记录,将它们传递给solr进行索引,然后处理下一个1000等等...
我目前的代码是这样做的:
Model.all.each do |r|
Sunspot.index(r)
end
Run Code Online (Sandbox Code Playgroud)
对于具有大约150万条记录的集合,这会占用8 GB以上的内存并导致该过程失败.在ActiveRecord中,有一个find_in_batches方法,允许我将查询分块为可管理的批处理,以防止内存失控.但是,我似乎无法为mongoDB/mongoid找到这样的东西.
我希望能够做到这样的事情:
Model.all.in_batches_of(1000) do |batch|
Sunpot.index(batch)
end
Run Code Online (Sandbox Code Playgroud)
这样可以通过每次只进行一次可管理的问题集来缓解我的记忆问题和查询困难.但是,在mongoDB中进行批量查找时,文档很稀疏.我看到很多关于批量插入但没有批量查找的文档.
谁能向我解释mongoid embeds_many
和has_many
mongoid 之间的区别?