我是Mongo DB和Mongoid的新手(并且仍然是Ruby on Rails的新手).由于Ryan Bates没有碰巧有一个Mongoid Railscast,我需要指向其他好的教程/截屏视频.谢谢!
我想使用MongoDB 的addToSet方法,但Mongoid目前还不支持这个.有没有办法直接从我的Rails模型访问MongoDB驱动程序?
在我的rails应用程序中我使用mongoid并在日志之前几乎每个查询,即使在相同的请求,它也
MONGODB dbname['system.namespaces'].find({})
Run Code Online (Sandbox Code Playgroud)
这是做什么的?这是性能问题吗?我可以以某种方式缓存,所以它不必一直这样做或至少阻止它堵塞日志?
编辑:这是日志的相关部分
Processing by FilesController#new as HTML
Started GET "/" for 127.0.0.1 at Fri Sep 09 15:59:43 -0700 2011
[Barista] Compiling all scripts for barista
[Barista] Compiling all coffeescripts
[Barista] Copying all javascripts
MONGODB db['system.namespaces'].find({})
MONGODB db['users'].find({:_id=>BSON::ObjectId('4e6a949935d3e9726b000001')})
MONGODB db['system.namespaces'].find({})
MONGODB db['files'].find({:token=>"nonssb38"})
Run Code Online (Sandbox Code Playgroud) 我是usig"newrelic_rpm gem",它在sqllite中运行良好(即,在rails中嵌入数据库).当使用mongoid.yml时,它显示mongoid的sql [0].它也无法在http://localhost.com:3000/newrelic/show_sample_sql?id=91570930中列出mongoid查询.
新遗物是否支持mongoid?在新文物申请中提供包括mongoid的步骤..,提前谢谢
所以我有两个这样的模特
class ModelParent
include Mongoid::Document
field :name, :type => String
has_one :model_child
end
class ModelChild
include Mongoid::Document
field :name, :type => String
belongs_to :model_parent
end
Run Code Online (Sandbox Code Playgroud)
假设我在rails控制台中有一个名为mp的持久化ModelParent实例
mc = mp.create_model_child(:name=>"child")
Run Code Online (Sandbox Code Playgroud)
然后呢
mp.model_child
Run Code Online (Sandbox Code Playgroud)
它返回一个有效的对象
但是,如果我这样搜索它:
ModelParent.where(:model_child.ne => nil).length
Run Code Online (Sandbox Code Playgroud)
它返回0
我已经尝试创建model_child然后使用build_model_child()进行分配,并且每个方法都显示model_child显然位于父级中,但是非nil(.ne)的查询无法找到所有带子级的ModelParents.
我究竟做错了什么?
更新:
回答我自己的问题.我仍然不确定为什么:model_child.ne => nil不起作用,但是......
我用这样的代码解决了这个问题:
def self.with_child
user_ids = ModelChild.all.only(:model_parent_id).map(&:model_parent_id)
return ModelParent.where(:_id.in => user_ids).all
end
Run Code Online (Sandbox Code Playgroud) 在mongoid中是否有任何方法可以找到并"读取"集合中的所有文档,同时还可以在一个原子查询中删除它们?
到目前为止我一直在使用:
Model.collection.find().to_json
Model.delete_all
Run Code Online (Sandbox Code Playgroud)
通过在这两个指令之间添加更多数据来收集,可以轻松解决这个问题.
我最近使用rails 3.2.12和ruby 1.9.3从mongoid 2.0.2升级到mongoid 3.
Following issue comes when save command excutes => @new_node.save
Moped::Errors::OperationFailure (The operation: #<Moped::Protocol::Command
@length=366
@request_id=30
@response_to=0
@op_code=2004
@flags=[:slave_ok]
@full_collection_name="campus_dev.$cmd"
@skip=0
@limit=-1
@selector={:aggregate=>"nodes", :pipeline=>[{"$match"=>{"parent_id"=>"51382df8851d1912b7000009", "_id"=>{"$ne"=>"513f24952f1feda4bc000002"}, "position"=>{"$nin"=>[nil]}}}, {"$group"=>{"_id"=>"position", "count"=>{"$sum"=>1}, "max"=>{"$max"=>"$position"}, "min"=>{"$min"=>"$position"}, "sum"=>{"$sum"=>"$position"}, "avg"=>{"$avg"=>"$position"}}}]}
@fields=nil>
failed with error "no such cmd"):
app/controllers/nodes_controller.rb:37:in `create'
Run Code Online (Sandbox Code Playgroud) 每次我尝试在视图中使用Datetime_select时,应用程序都会抛出属性错误.
Mongoid::Errors::UnknownAttribute:
Problem:
Attempted to set a value for 'fromtime(1i)' which is not allowed on the model Event.
Summary:
Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Event#fromtime(1i)= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError.
Resolution:
You can include Mongoid::Attributes::Dynamic if you expect to be writing …Run Code Online (Sandbox Code Playgroud) 我的红宝石模型,如下:
class User
include Mongoid::Document
field :first_name, type: String
field :birthdate, type: Date
validates :first_name, :birthdate, :presence => true
end
Run Code Online (Sandbox Code Playgroud)
输出一个像这样的对象:
{
_id: {
$oid: "522884c6c4b4ae5c76000001"
},
birthdate: null,
first_name: null,
}
Run Code Online (Sandbox Code Playgroud)
我的骨干项目不知道如何处理_id.$ oid.
我发现这篇文章和代码:
https://github.com/rails-api/active_model_serializers/pull/355/files
module Moped
module BSON
class ObjectId
alias :to_json :to_s
end
end
end
Run Code Online (Sandbox Code Playgroud)
I have no idea where to put this, and how to invoke it on the model output, so I tried inside:
/config/initializers/secret_token.rb
I'm new to Ruby and Rails and have no idea …
我正在使用elasticsearch -rails和mongoid 我有以下简单的字段映射:
"title": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的模型看起来像这样:
class ArticlesEvent
include Mongoid::Document
include Elasticsearch::Model
field :title, type: String
attr_accessible :title
def as_indexed_json(options={})
as_json(except: [:id, :_id])
end
Run Code Online (Sandbox Code Playgroud)
任何人都可以举例说明如何使用title.raw字段定义rails模型,以及如何访问该字段?由于多字段已被弃用,因此很难找到使用rails和mongoid的工作示例.
谢谢