草庐IT

mongoid4

全部标签

ruby-on-rails - Rails 和 Mongoid 的动态属性

我正在通过带有Rails(Rails3beta3)的MongoidRubygem来学习MongoDB,并且我试图想出一种方法来基于来自另一个模型的字段在模型上创建动态属性,我认为无模式数据库将是一个不错的选择。例如,我有模型:classAccountincludeMongoid::Documentfield:name,:type=>Stringfield:token,:type=>Stringfield:info_needed,:type=>Arrayembeds_many:membersendclassMemberincludeMongoid::Documentembedded_in

ruby-on-rails - heroku mongohq 和 mongoid Mongo::ConnectionFailure

2012年6月9日更新:在heroku使用mongoid3.0.0.rc进行设置,请参阅以下要点:https://gist.github.com/29008042011年1月22日更新:Uri现在优先于mongoid.ymlhttps://github.com/mongoid/mongoid/issues/issue/2662010年8月12日更新:虽然我在5月6日从JackuesCrocker那里得到了一个接受的答案,但这个问题的某些方面很容易搞砸!它再次发生在我身上,我决定研究mongoid源代码。所以,就这样吧:目前,host:port:name/database:setting

ruby-on-rails - 如何在 Ruby on Rails 环境中使用 Mongoid 进行通配符搜索?

Mongoid文档只给出了一个通配符搜索的例子:Person.where(first_name:/^d/i)这会查找所有名字以“d”开头的人。/^和/i代表什么?如何找到所有名字在字符串中间带有“na”的人?例如,此查询将找到“jonathan”,因为“na”是整个字符串的子字符串。是否有包含此信息的网站或指南? 最佳答案 你需要这个来找到名字中带有“na”的人。Person.where(first_name:/na/i)至于你的例子:Person.where(first_name:/^d/i)^表示“行首”。此正则表达式将匹配第一

ruby-on-rails - 在 Rails 中使用 Mongoid 的 MongoDB - 地理空间索引

MongoDB有一个很好的GeospatialIndexing特征。如何在带有Mongoid的Rails中使用它? 最佳答案 你可以在mongoid中定义这样的地理索引classItemincludeMongoid::Documentfield:loc,:type=>Arrayindex([[:loc,Mongo::GEO2D]],background:true)end对于查询$near命令(不带maxDistance)location=[80.24958300000003,13.060422]items=Item.where(:l

ruby-on-rails - 如何使用 MongoDB 和 Mongoid 在 Rails 3 上进行适当的数据库测试 (TDD)

如何通过MongoidonRails使用MongoDB编写适当的单元测试(和集成测试)?我在问,因为与使用SQLite3相反,即使在运行测试时,我所做的一切都会持续存在。所以目前我正在编写创建测试,然后我手动删除我所做的一切。但是对于集成测试来说,它变得越来越烦人甚至变得复杂。我所做的示例:before(:each)do@user=User.create!(@attr)endafter(:each)do#MongoDBisnotatransactionalDB,soaddedobjects(create)duringtestscan'tberollbacked#checkingfort

ruby - 如何在 Mongoid 中引用嵌入的文档?

使用Mongoid,假设我有以下类:classMapincludeMongoid::Documentembeds_many:locationsendclassLocationincludeMongoid::Documentfield:x_coord,:type=>Integerfield:y_coord,:type=>Integerembedded_in:map,:inverse_of=>:locationsendclassPlayerincludeMongoid::Documentreferences_one:locationend如您所见,我正在尝试为一个简单的游戏世界环境建模,其

ruby-on-rails - 在 mongoid 中使用 LIKE/regex 和变量

我正在尝试查找文本中包含单词test的所有文档。以下工作正常:@tweets=Tweet.any_of({:text=>/.*test.*/})但是,我希望能够搜索用户提供的字符串。我认为下面的方法会起作用,但它不起作用:searchterm=(params[:searchlogparams][:searchterm])@tweets=Tweet.any_of({:text=>"/.*"+searchterm+".*/"})我已经尝试了我能想到的一切,任何人都知道我可以做些什么来完成这项工作。提前致谢。 最佳答案 searchter

ruby-on-rails - Mongoid 有 Map/Reduce 吗?

我正在使用Ruby代码从Mongoid返回的数组中计算总和。但也许使用Map/Reduce会更快,除了我在mongoid.org和Google上没有看到任何MapReduce文档mapreducesite:mongoid.org也不给出任何结果。(或使用MapReduce或Map/Reduce)MongoDB网站上有文档mapreducesite:mongodb.org但也需要将MapReduce与Mongoid一起使用。 最佳答案 您可以将mapreduce与Mongoid一起使用,就像直接通过Ruby驱动程序一样:#Postis

ruby-on-rails - Mongoid 有 Map/Reduce 吗?

我正在使用Ruby代码从Mongoid返回的数组中计算总和。但也许使用Map/Reduce会更快,除了我在mongoid.org和Google上没有看到任何MapReduce文档mapreducesite:mongoid.org也不给出任何结果。(或使用MapReduce或Map/Reduce)MongoDB网站上有文档mapreducesite:mongodb.org但也需要将MapReduce与Mongoid一起使用。 最佳答案 您可以将mapreduce与Mongoid一起使用,就像直接通过Ruby驱动程序一样:#Postis

ruby-on-rails - 与 Mongoid 聚合

MongoDB有一个新的AggregationFramework我正在尝试弄清楚如何将它与Mongoid一起使用。似乎有一个具有此功能的Moped分支,正如所讨论的here.我已经更新到MongoDB2.2并尝试在我的应用程序上安装这个Moped分支,如下所示:gem'moped',git:'git://github.com/mongoid/moped.git',branch:'aggregation-support'但聚合仍然无法正常工作。这是我用来测试它的调用:=Post.all.aggregate({"$group"=>{"_id"=>"$_id"}})更新在mongoshell