Tag: mongodb

MongoDB不会使用Mongoid连接到MongoHQ

我刚刚开始了一个全新的rails项目,我正在尝试完成的第一项任务是创建一个对象并将其保存在我的数据库中。 我经历了生成对象的自动方式,在这种情况下是一个URL对象,如下所示: rails generate scaffold Url domain:string 我还测试了两个单独的mongoid.yml配置设置。 第一个如下: development: sessions: default: # Defines the name of the default database that Mongoid can connect to. # (required). uri: mongodb://[username]:[password]@flame.mongohq.com:27046/[database] options: consistency: :strong 此配置的结果是,当发生查询时,我收到以下错误: The operation: # failed with error 13075: “db name can’t be empty” 我还测试了以下配置: development: uri: mongodb://[username]:[password]@flame.mongohq.com:27046/[database] 当代码到达urls_controller的create方法时,它在@ url.save失败,并出现以下错误: mongoid (3.0.0.rc) lib/mongoid/sessions/factory.rb:100:in `parse’ […]

Mongoid:使用多个数据库?

我在rails应用程序中使用MongoDB和MongoID,某些模型如何成为同一服务器上不同mongo数据库的一部分? 我怎么能做到这样的事情? 我以前遇到过与mysql相同的问题,无法找到合理的解决方案。 有什么想法吗?

为什么rails试图连接到mysql?

我一直在新的rails应用程序中使用mysql,但现在我想尝试mongoDB,所以我安装了mongo mapper和mongoid(使用mongo session)。 安装似乎很好,因为我可以创建mongo模型。 但由于某些原因,rails仍在尝试连接到mysql: Can’t connect to local MySQL server 。 这太可怕了,因为即使我没有使用mongo,rails也不应该为每个请求尝试连接到mysql。 即使对于不存在的URL,它也会抛出该错误。 我该怎么做才能调试这个? 我想我可以尝试从Gemfile中删除mysql gem并运行bundle install 。 但即使我不使用它,我仍然不喜欢它试图连接的事实。 它不应该尝试连接’懒惰’(即:仅按需)? development.rb: Myapp::Application.configure do # Settings specified here will take precedence over those in config/application.rb # In the development environment your application’s code is reloaded on # every request. This slows down response time but […]

Mongoid store_in产生随机结果

我正在使用Rails 3.2.2和mongoid 2.4.6。 为了保持我的集合小,我使用“store_in”语句将子对象存储在sepparate集合中的基类中。 我的代码如下所示: class BaseClass include Mongoid::Document end class ChildClass1 < BaseClass store_in :child_1 end class ChildClass2 < BaseClass store_in :child_2 end 似乎对象随机存储在或其他子集合中。 Child1类型的对象有时会存储在集合Child2中。 以下是我在日志中看到的令人惊讶的事情: Started POST “/child_class_1” for 127.0.0.1 at 2012-05-22 10:22:51 -0400 Processing by ChildClass1Controller#create as HTML MONGODB (0ms) myproject_development[‘child_2’].insert…. 它来自哪里? 这是mongoid,rails还是mongodb中的错误?

Rails Mongoid无法进行身份validation – 失败,错误13:“未授权查询my_db.my_collection”

据说这个问题已解决了最新版本的轻便摩托车但仍然在我身上发生。 我有一个带有Mongoid的rails 4.2应用程序,为具有readWrite和dbOwner角色的MongoDB DB创建了一个用户,并在mong.conf文件中设置了auth = true 。 我可以使用Mongo shell或使用Mongo驱动程序的简单Java应用程序使用该用户凭据在DB上执行任何操作。 但是,当尝试使用Mongoid进行身份validation时,我总是会收到此错误: 失败,错误13:“未授权查询my_db.my_collection” 这是我的mongoid.yml文件的相关部分: production: # Configure available database sessions. (required) sessions: # Defines the default session. (required) default: # Defines the name of the default database that Mongoid can connect to. # (required). database: my_db hosts: – localhost:27017 username: my_username password: my_password 我也尝试用服务器的远程地址替换主机并远程访问它(这与禁用的身份validation选项一起使用)没有成功。 为了它的价值,我可以通过调试mpped / node.rb文件看到凭证,在ensure_connected方法中我看到@credentials变量包含我的用户名和密码我在这里缺少什么? […]

使用长度标准查询MongoDB

我在MongoDB集合中有几个文档,带有字段’name’(这是一个String)。 如何执行7 <= name.length <= 14

Rails 4 / Devise / MongoDB:使用自定义属性和强参数的“未允许的参数”

尝试将嵌套的自定义属性Profile (一个Mongoid文档)添加到我的设计用户类。 提交Devise注册表单时,它还应创建用户和相应的Profile对象。 我希望最终结果在我的MongoDB中看起来像这样: 用户: { # Devise fields: “email”: “my@email.com”, … # Custom field “profile” : “” } 轮廓: { “first_name”: “Dave”, …. } 不幸的是,每当我提交注册时,我都会在控制台中收到此消息。 它成功创建了一个用户,但无法创建关联的配置文件。 Started POST “/” for 127.0.0.1 at 2013-04-20 23:37:10 -0400 Processing by Users::RegistrationsController#create as HTML Parameters: {“utf8″=>”✓”, “authenticity_token”=>”awN2GU8EYEfisU0”, “user”=> {“profile_attributes”=> {“first_name”=>”Dave”, “birthday(2i)”=>”4”, “birthday(3i)”=>”21”, “birthday(1i)”=>”1933”, “occupation_title”=>”Software Developer”}, “password”=>”[FILTERED]”, “password_confirmation”=>”[FILTERED]”, “email”=>”my@email.com”}} […]

在分析由Mongoid事件生成的ActiveRecord对象时,Rspec测试随机失败

我实现了一个基于Mongoid的活动日志记录机制,可以在MongoDB中保存事件。 Mongoid模型Activity具有after_create事件,根据记录的活动类型执行不同的任务:(简化示例) class Activity include Mongoid::Document after_create do |activity| method_name = “after_#{activity.event_type}” send(method_name) if respond_to? method_name end def after_user_did_something MyItem.create!(:type => :user_did_something) end end 测试看起来像这样: it ‘should hide previous [objects] create a new updated one’ do 2.times do user.log_activity(:user_did_something) end items = MyItems.where(:type => :user_did_something) items.count.should == 2 end end 有时,测试在items.count为0而不是2时失败。只有当从命令行运行时才会发生这种情况。当运行此测试时,或者在使用Guard运行所有测试时,它永远不会发生。

具有Mongoid和Rails的字段别名

MongoDB中的常见做法是使用短密钥名称来节省空间。 例如,有人可能想使用“fn”而不是“first_name” 然而,在你的应用程序中,如果你在整个地方使用“fn”,你就会被搞砸了。 太难看了。 特别是对于Rails,在Mongoid中声明字段时是否有一种简单的方法来指定别名? 另外,有没有人知道任何使用Mongoid的开源示例项目? 谢谢!

如何直接从Ruby查询MongoDB而不是使用Mongoid?

我正在编写一个使用MongoDB和Mongoid的Rails应用程序的迁移。 我的迁移目前使用我的模型使用Mongoid查询和更新记录,但性能低于标准。 我本质上是更新大型集合中的所有记录并进行n + 20次查询。 我花了一个小时在本地运行(并没有完成)后杀死了迁移。 我希望能够毫不费力地将原始查询运行到mongo。 我假设有一些方法可以从Mongoid访问mongo驱动程序,因为Mongoid已经加载了与数据库的连接。 如何访问数据库以直接运行更新查询?