Tag: mongodb

使mongoid会话只读

我在mongoid.yml有不同的会话,其中一个会话提供来自静态mongo数据库的数据。 我想知道是否有可能在只读模式下“加载”会话,因此不能对save , create , destroy或destroy_all进行任何更改。 我的mongoid.yml看起来像这样: production: sessions: default: database: my_app_production hosts: – localhost:27017 options: read: primary static_content: database: static_content_db hosts: – localhost:27017 options: read: primary options: use_utc: true 我有static_content会话的特殊模型,它们看起来像这样: class StaticMappings include Mongoid::Document include Mongoid::Attributes::Dynamic store_in collection: “static_mappings”, session: “static_content” end 我想防止自己形成意外调用StaticMappings.destroy_all或StaticMappings.create(…) 。 这可能吗? 我发现这使用Mongoid将整个模型设为只读 ,但这不会阻止某人在模型实例上调用create或destroy 。

如何使用Mongoid更新Rails中的嵌套Mongo文档属性?

(如果这个问题的细节很短,请提前道歉,我会看评论并添加我能做的) 我有一个带有以下内容的模型: class Product include Mongoid::Document include Mongoid::Timestamps #… field :document_template, :type => Document accepts_nested_attributes_for :document_template 在document document_template中,是以下references_many,我想修改它。 具体来说,我想更改引用的字体: class Document include Mongoid::Document include Mongoid::Timestamps #… references_many :fonts, :stored_as => :array, :inverse_of => :documents 我应该在控制器和表单中使用什么样的逻辑和细节来完成这项工作? 如果您希望我添加一些我尝试过的东西,请发表评论; 但是,我没有任何运气。 以下是使用rails console快速显示的问题: # Grab a Product and check how many fonts are in it’s document_template ruby-1.8.7-p302 > prod […]

mongodb mongoid-rails无法在嵌入文档的散列中增加计数器

我使用的是Mongoid 4.0.2,Rails 4.2.1和Mongodb 2.4.8。 我将daily_events作为人员集合中的嵌入文档,如下所示。 class Person include Mongoid::Document field :email, type: String field :month, type: Date field :monthly_total, type: Hash, default: {:email_open => 0, :email_click => 0, :page_view => 0} embeds_many :daily_events end class DailyEvent include Mongoid::Document field :name, type: String field :day_1, type: Hash, default: -> {:email_open => 0, :email_click => 0, :page_view […]

如何搜索记录然后将其删除

如何通过手机号搜索ttoken记录。 然后删除该记录? User.rb: field :ttokens, type: Hash, :default => {} # Stored as hash over numbers 通常,rails控制台中的ttokens值如下: ttokens: {“919839398393″=>{“atoken”=>”f704e803061e594150b09ad8akabfc6105ac85ab”, “confirmed”=>true}, “91812798765”=>{“atoken”=>”255cb70926978b93eik67edb52fa23a163587b4b”, “confirmed”=>true}} 我需要一个mongodb查询来搜索手机号码的ttoken记录,然后删除该记录。 使用的数据库是MongoDB。 任何指导或帮助将受到高度赞赏。

Ruby on Rails Mongoid和Webfaction:未授权查询(错误16550)

我使用的是2.4.4版,遵循http://docs.webfaction.com/software/mongodb.html上的程序,并使用Mongoid和Ruby on Rails。 我还在我正在使用的db中使用“userAdminAnyDatabase”权限创建了一个用户,并将其与此rails mongoid配置一起使用: production: sessions: default: database: hosts: – localhost: username: password: 我有服务器使用–auth标志运行,我也尝试使用mongodb cpmmand行的用户,它工作但我部署了我的Rails应用程序,我仍然得到: 失败,错误16550:“未授权查询 有什么我想念的吗? 也许我需要创建一个特殊的用户?

Mongoid`group()`条件

我想为Mongoid中的分组提供条件,但是如何在条件哈希中为属性发送多个值? 这就是我想要做的: PageViews.collection.group( cond: {page_id: [‘4e6912618083ab383e000010’, ‘4e6912618083ab383e000009’]}, key: ‘timestamp’, initial: {count: 0}, reduce: “function(x, y) {y.count += x.count;}” ) 因此,任何带有page_id PageView都将是查询的一部分,但我似乎无法使条件哈希( cond )完全起作用! 我在这里做错了什么,我真的很困惑。 以下是group()方法的API文档: http : //api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method 任何帮助将不胜感激。 更新 像这样运行查询: PageViews.collection.group( cond: {page_id: { $in : [‘4e6912618083ab383e000010’, ‘4e6912618083ab383e000009’]}}, key: ‘timestamp’, initial: {count: 0}, reduce: “function(x, y) {y.count += x.count;}” ) 返回以下错误,但语法对我来说很好: SyntaxError: (irb):170: syntax […]

使用MongoMapper创建用于编辑嵌入文档的表单

我正在玩MongoMapper,但我无法弄清楚如何为具有嵌入文档的对象创建表单。 使用ActiveRecord,我会使用fields_for ,但当被问及几个月前是否会支持这种情况时, MongoMapper的作者John Nunemaker写道 :“没有,也没有。对于attr_accessor来说真的很难。” 好的,公平的,但你怎么写这个表格来工作呢? 我对使用那里的嵌套表单实现不感兴趣,因为我想以“正常”方式执行此操作,因为我正在学习MongoMapper。 我的模型很简单 – 我有一个人员,其中包含用于电子邮件地址,电话号码等的嵌入式文档。我不关心更新现有的嵌入式文档。 每次编辑Person时,都可以从表单输入中重新创建它们。

如何在mongoid中强制使用独特的嵌入式文档

我有以下型号 class Person include Mongoid::Document embeds_many :tasks end class Task include Mongoid::Document embedded_in :commit, :inverse_of => :tasks field :name end 我该如何确保以下内容? person.tasks.create :name => “create facebook killer” person.tasks.create :name => “create facebook killer” person.tasks.count == 1 different_person.tasks.create :name => “create facebook killer” person.tasks.count == 1 different_person.tasks.count == 1 即任务名称在特定人员中是唯一的 检查了索引上的文档后,我认为以下内容可能有效: class Person include Mongoid::Document embeds_many […]

Mongoid:如何查询value为nil的所有对象?

我很难做一些事情,比如: Something.where(:field => nil) 要么 Something.where(:field => { ‘$eq’ => nil }) 什么是在Mongoid中处理这个问题的正确方法?

需要设计艺术家推荐的建议

我有以下场景。我需要向用户推荐艺术家。 该如何建模? 我正在寻找有关这方面的建议。 我刚在想。 基于以下标准: 当用户收听歌曲时,歌曲类型会以+1记录 当用户“喜欢”一首歌时,歌曲类型会以+2记录在某处 通过这种方式,我可以根据具有最高分的类型列出所有艺术家。 即使如此,我是否应该有一个名为“推荐”的新模型并将其嵌入到用户中? 我愿意接受各种建议。