草庐IT

mongoid3

全部标签

ruby-on-rails - 如何根据多个条件查找嵌入式 Mongoid 文档?

我有一个包含嵌入式文档的Mongoid文档。我想在顶级文档中搜索所有具有多个条件的嵌入式文档。TopDoc.where('inside.first_name'=>'Bob','inside.last_name'=>'Jones')但在我看来,这会与BobWever和PaulJones在TopDoc上匹配,这是错误的。 最佳答案 您需要使用$elemMatch.对于Mongoid,以下行应该可以解决问题TopDoc.elem_match(inside:{first_name:'Bob',last_name:'Jones'})相当于:T

ruby - 使用 Mongoid 检索嵌入式文档

我有一个名为Equipment的Mongoid文档,它可以嵌入多个问题文档。以下是文档架构:classEquipmentincludeMongoid::DocumentincludeMongoid::Timestampsfield:descriptionfield:modelNumberfield:titlefield:categoryIdfield:subCategoryIdfield:stateIdfield:cityfield:askingPriceembeds_many:questionsendclassQuestionincludeMongoid::Documentfield

ruby-on-rails - Rails Mongoid 包含不加载子模型

我不知道它是否应该像我期望的那样工作,但我认为:defshowrenderjson:Book.includes(:genres).find(params[:id])end应在图书模型中包含流派。classBookincludeMongoid::Document...has_and_belongs_to_many:genresendclassGenreincludeMongoid::Documentfield:name,type:Stringend但在客户端上,in仅包含流派ID列表。genre_ids:Array[1]0:Object$oid:"53532d3b616c6439c107

ruby-on-rails - Mongoid 5.1 中的查询日期范围

如何使用Mongoid5.1查询日期范围?我正在尝试类似的东西Data.find(sid:7,:created_at=>(Time.parse('5-07-2015')..Time.now))但这给了我一个错误有什么想法吗? 最佳答案 你可以只使用:Data.where(:created_at=>(Time.parse('5-07-2015')..Time.now))或者在你的情况下,因为它的范围是从5-07-2015到现在,那么你可以这样做Data.where(:created_at.gte=>Time.parse('5-07-2

ruby - 在mongoid中查询和排序嵌入式文档

我有三个类(class)classPostincludeMongoid::DocumentincludeMongoid::Timestampsbelongs_to:user,:inverse_of=>nilembeds_many:comments,:as=>:commentablefield:content,:type=>StringendclassCommmentincludeMongoid::DocumentincludeMongoid::Timestampsbelongs_to:user,:inverse_of=>nilembedded_in:commentable,:polym

ruby-on-rails - Mongoid:使用多个数据库?

我在Rails应用程序中使用MongoDB和MongoID,某些模型如何成为同一服务器上不同mongo数据库的一部分?我将如何完成这样的事情?我以前用mysql遇到过同样的问题,一直找不到合理的解决办法。有什么想法吗? 最佳答案 最新版本的Mongoid支持这个。参见thedocs.片段:配置/mongoid.yml:defaults:&defaultshost:localhostslaves:-host:localhostport:27018-host:localhostport:27019databases:secondary:

mongodb - 通过在 rails 中使用 mongoid 进行分组

我在rails4中使用mongoid,我需要按结果分组来提供图表(统计屏幕),因为我正在使用下面的代码。@comments_stats={}comments_data=Comment.where(:created_at.lte=>Date.today-10.days).group_by{|d|d.created_at.to_date}comments_data.map{|a|@comments_stats[a[0].strftime("%d-%m-%y")]=a[1].size}它会像{"1-1-2014"=>2,"3-1-2014"=>1,"4-1-2014"=>2,"6-1-201

ruby-on-rails - MongoDB 不会使用 Mongoid 连接到 MongoHQ

我刚刚开始了一个全新的Rails项目,我要完成的第一个任务是创建一个对象并将其保存在我的数据库中。我经历了生成对象的自动化方式,在本例中是一个URL对象,如下所示:railsgeneratescaffoldUrldomain:string我还测试了两个单独的mongoid.yml配置设置。第一个如下:development:sessions:default:#DefinesthenameofthedefaultdatabasethatMongoidcanconnectto.#(required).uri:mongodb://[username]:[password]@flame.mon

ruby-on-rails - Mongoid:如何验证哈希字段上特定键的值?

在Mongoid中,我可以为一个类型为Hash的字段的属性设置一个索引,如下所示:index({"details.fullName"=>1},{name:"full_name_index"})我想做的是使用如下内容为此类实体创建验证:validates"details.fullName",presence:true不幸的是,这会产生以下错误:NoMethodError:undefinedmethod`details.fullName'forThing是我犯了语法错误,还是Mongoid3根本不支持此功能? 最佳答案 由于Hash数据

ruby-on-rails - Mongoid Group By 或 MongoDb group by in rails

我有一个mongo表,其中包含如下统计数据......类(class)编号status这是一个字符串,已播放或已完成和时间戳信息使用Mongoid'sTimestampingfeature所以我的类如下...classStatisticincludeMongoid::DocumentincludeMongoid::TimestampsincludeMongoid::Paranoiafield:course_id,type:Integerfield:status,type:String#currentlythisiseitherplayorcomplete我想获得一门类(class)的每