希望这个标题非常明显.
我正在使用mongoidRails应用程序作为我的ORM,我想知道是否有人知道它是否与ActiveRecord的serialize方法等效.我查看了mongoid文档,但还没有找到任何东西.
以下是该模型的示例:
class Foo
include Mongoid::Document
field :params, type: String
serialize :params # method from ActiveRecord
end
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我们有一个带有嵌入式项目的模型条目:
class Entry
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Spacial::Document
embeds_many :items, cascade_callbacks: true
...
class Item
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Spacial::Document
embedded_in :entry
...
Run Code Online (Sandbox Code Playgroud)
如果我按项目ID直接查询mongo:
{"items._id" : ObjectId("50536b18baa072000f000360")}
Run Code Online (Sandbox Code Playgroud)
它返回条目:
505363b36181ce00020006b1 {"created_at":"2012-09-14T17:04:51Z","items":[{"_ id":"50536b1a2b17b3 ...
然而,当我通过Mongoid查询时:
irb(main):002:0> Entry.where('items._id' => '50536b18baa072000f000360')[0]
=> nil
Run Code Online (Sandbox Code Playgroud)
所有其他查询都有效(对于项目和输入字段的其他字段).但不是为了id.
我们正在运行mongoid(2.4.12).
样本文件:
{
_id: ObjectId('4f442120eb03305789000000'),
time: ISODate("2013-10-10T20:55:36Z"),
value:1
},
{
_id: ObjectId('4f442120eb03305789000001'),
time: ISODate("2013-10-10T28:43:16Z"),
value:2
},
{
_id: ObjectId('4f442120eb03305789000002'),
time: ISODate("2013-10-11T27:12:66Z"),
value:3
},
{
_id: ObjectId('4f442120eb03305789000003'),
time: ISODate("2013-10-11T10:15:38Z"),
value:4
},
{
_id: ObjectId('4f442120eb03305789000004'),
time: ISODate("2013-10-12T26:15:38Z"),
value:5
}
Run Code Online (Sandbox Code Playgroud)
可以轻松获得按日期分组的聚合结果.但我想要的是查询返回聚合的运行总计的结果,如:
{
time: "2013-10-10"
total: 3,
runningTotal: 3
},
{
time: "2013-10-11"
total: 7,
runningTotal: 10
},
{
time: "2013-10-12"
total: 5,
runningTotal: 15
}
Run Code Online (Sandbox Code Playgroud)
这可能与MongoDB聚合有关吗?
ORM mongo_mapper和mongoid都支持rails上的ruby.但是,在阅读完他们的文档后,我仍然无法决定用哪个来开发一个带有rails4的新应用程序.
有什么建议吗?
我有一个工作,为我的文档创建新的字段,我希望,在这个工作结束时,为这个字段创建索引.我试过了
Model.index("field"=>-1)
Run Code Online (Sandbox Code Playgroud)
并且
Mongoid::Sessions.default[:rating_prediction].ensureIndex
Run Code Online (Sandbox Code Playgroud)
没有成功
这可能吗?
假设我有一个用户集合.有没有办法使用mongoid在集合中找到n个随机用户,它不会返回同一个用户两次?现在让我们假设用户集合如下所示:
class User
include Mongoid::Document
field :name
end
Run Code Online (Sandbox Code Playgroud)
简单吧?
谢谢
我在我的模型中定义了以下范围:
scope :upcoming, -> { where(:start_time.gt => Time.now).asc(:start_time) }
scope :in_progress, -> {
now = Time.now
where(:start_time.lte => now).where(:end_time.gte => now).asc(:start_time)
}
Run Code Online (Sandbox Code Playgroud)
我想创建另一个范围,它结合了两个名为current的范围的结果.我试过这样的事情:
scope :current, -> { self.in_progress | self.upcoming }
Run Code Online (Sandbox Code Playgroud)
但这最终只会将它们视为数组并将它们连接起来.这个问题是,当我尝试使用Model.current调用我的作用域时,我收到以下错误消息:
NoMethodError: undefined method `as_conditions' for #<Array:0xaceb008>
Run Code Online (Sandbox Code Playgroud)
这是因为它将Mongoid Criteria对象转换为数组,但我不希望这样.我希望该对象保留为Mongoid Criteria对象.
我真正想要的是in_progress集和即将到来的集合的结合.
有任何想法吗?谢谢.
如果首先使用belongs_to和has_many关联构建模型,然后意识到他们需要转移到embedded_in和embeds_many关联,那么如何在不使数千条记录失效的情况下执行此操作?需要以某种方式迁移它们.
我理解这不是一个编程问题,我无法找到一个非常清晰和描述性的解决方案.
Rails 4.2.5, Mongoid 5.1.0
我有三个型号- Mailbox,Communication和Message.
mailbox.rb
class Mailbox
include Mongoid::Document
belongs_to :user
has_many :communications
end
Run Code Online (Sandbox Code Playgroud)
communication.rb
class Communication
include Mongoid::Document
include Mongoid::Timestamps
include AASM
belongs_to :mailbox
has_and_belongs_to_many :messages, autosave: true
field :read_at, type: DateTime
field :box, type: String
field :touched_at, type: DateTime
field :import_thread_id, type: Integer
scope :inbox, -> { where(:box => 'inbox') }
end
Run Code Online (Sandbox Code Playgroud)
message.rb
class Message
include Mongoid::Document
include Mongoid::Timestamps
attr_accessor :communication_id
has_and_belongs_to_many :communications, autosave: true
belongs_to :from_user, class_name: 'User'
belongs_to …Run Code Online (Sandbox Code Playgroud) ruby-on-rails mongodb mongoid mongodb-query aggregation-framework