草庐IT

ruby - 是否可以指定查询应在 Mongoid 中使用的索引?

当一个索引是另一个索引的子集时,MongoDB似乎使用了一种低效的查询模式。classModelfield:status,:type=>Integerfield:title,:type=>Stringfield:subtitle,:type=>Stringfield:rating,:type=>Floatindex([[:status,Mongo::ASCENDING],[:title,Mongo::ASCENDING],[:subtitle,Mongo::ASCENDING],[:rating,Mongo::DESCENDING]])index([[:status,Mongo::AS

ruby - 带有 eventmachine 的 mongoid 和 mongomapper

我对eventmachine很陌生,我无法在网上找到这个问题的答案。现在我一直在使用带有eventmachine的em-mongo,而且我基本上已经在文档和它们各自的对象之间编写了一个穷人的映射器。我只是想确认mongoid和mongomapper不能在eventmachine环境中使用,因为它们是阻塞的,因此首先破坏了使用eventmachine的点,而且我没有浪费时间编写这些映射器。:P 最佳答案 EventMachine同步可能是您想要的:https://github.com/igrigorik/em-synchrony它支持

ruby-on-rails - 具有默认顺序的 Mongoid embeds_many

如何为我的嵌入对象设置默认顺序,例如:classPostembeds_many:comments,:order=>"author"accepts_nested_attributes_forend现在我直接通过订单来处理它:f.fields_for:comments,@post.comments.asc(:author)do|comment|...end 最佳答案 在mongoid3.1.2中你可以这样做:embeds_many:favorites,order::title.desc它也适用于:title.asc

ruby-on-rails - Rails 3 find_or_create 由多个属性 mongoid

在此链接Railsfind_or_createbymorethanoneattribute?可以在事件记录中使用多个属性。如何在mongoid中使用多个属性?谢谢 最佳答案 如果你查看lib/mongoid/finders.rb中的源代码:#Findthefirst+Document+giventheconditions,orcreatesa#withtheconditionsthatweresupplied....#@param[Hash]attrsTheattributestocheck.##@return[Document]A

mongodb - mongoid 如何为测试和生产环境创建索引

mongoid如何为测试和生产环境创建索引?我是说rakedb:mongoid:create_indexes=>environment如何设置环境?我使用mongoid2.4.9和rails3.2.7。 最佳答案 与Rails的其他rake任务一样,只需在命令行上指定RAILS_ENV即可,例如,rakedb:mongoid:create_indexesRAILS_ENV=test您可以在另一个窗口中跟踪您的日志文件以观察它发生的情况。tail-flog/*.log 关于mongodb-

collections - 如何使用 Mongoid 判断 MongoDB 中是否存在集合?

由于Mongoid.master.collection()会返回一个集合,即使该集合不存在,我们也可以使用coll=Mongoid.master.collection('analyticsCachedResult')ifcoll.count==0#[...]end测试它是否是一个空集合。另一种方法是循环遍历Mongoid.master.collections.eachdo|c|returncifc.name=='analyticsCachedResult'endreturnnil但是有没有更简单的方法来检测它是否存在? 最佳答案 不

mongodb - 如何使用 Mongoid 执行 $geoIntersects 查询?

我正在使用Sinatra和mongoid驱动程序,现在我正在尝试在mongoid中执行此查询,实际上我有一个名为'geometry的地理空间(多边形)字段':db.states.find({geometry:{$geoIntersects:{$geometry:{type:"Point",coordinates:[-99.176524,18.929204]}}}})其实这个查询是在mongodbshell中工作的。但是,我想找到与给定点(Point-in-polygon)与mongoid或其他ruby​​驱动程序相交的状态。任何帮助将不胜感激。谢谢。 最佳答

ruby - 使用 Mongoid 按日期分组的最佳方式

我正在尝试使用不同格式(日、月、年)按日期分组查询,我知道在典型的SQL数据库上这是一个简单的查询。您可以在此链接上查看代码:https://gist.github.com/jrdi/b3f824fa4e7531c43bfd知道我可以跑:>Patient.group_by('created_at','day')=>[{"_id":"11/10/2013","value":{"count":3.0}}]在我看来,所有这些用于制作简单分组的代码真的很奇怪。我错过了什么重要的东西吗?PD:我知道方法self.map和一些插值不是最好的方法,但我知道我关心Mongo的东西。

ruby-on-rails - mongoid 批量更新

我想每小时更新大量文档。这里是相当简单的模型:classArticleincludeMongoid::Documentfield:article_nr,:type=>Integerfield:vendor_nr,:type=>Integerfield:description,:type=>Stringfield:eanfield:stockfield:orderedfield:eta所以每小时我都会得到一份新的库存list,其中:stock,:ordered和:eta“可能”已经改变我需要更新它们。编辑:库存list只包含:article_nr,:stock,:ordered,:eta

ruby-on-rails-3 - mongoid 唯一索引允许重复值

我有一个以用户名作为字段的集合。模型将此字段定义为唯一的。但是我能够在数据库中插入一个重复的值。classProfileincludeMongoid::DocumentincludeMongoid::Paperclipfield:usernameindex({username:1},{unique:true})end但该集合有2个相同的用户名{"_id":ObjectId("50b3b323421aa95da6000004"),"username":"marceloreuse"}{"_id":ObjectId("50b3b567421aa93d84000002"),"username"