如果这是一个noob问题,请原谅我:
我有一个应用程序,用户可以在他们的个人资料中设置自己的时区.
当有人添加阵容(特定于应用程序的术语)时,我会执行以下操作:
time = ActiveSupport::TimeZone.new(user.timezone).parse(
"Wednesday, 26 October, 2011 13:30:00"
)
# This outputs: 2011-10-26 13:30:00 +0200 - valid according to the user selected TZ
Run Code Online (Sandbox Code Playgroud)
然后我保存了阵容:
Lineup.create({
:date => time.gmtime,
:uid => user._id,
:pid => product._id
})
Run Code Online (Sandbox Code Playgroud)
这应该(理论上)将日期保存为gmtime,但在查看记录时我会得到以下信息:
{
"_id": ObjectId("4e9c6613e673454f93000002"),
"date": "Wed, 26 Oct 2011 13: 30: 00 +0200",
"uid": "4e9b81f6e673454c8a000001",
"pid": "4e9c6613e673454f93000001",
"created_at": "Mon, 17 Oct 2011 19: 29: 55 +0200"
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,日期字段错误 - 它仍然保持用户时区,它应该是GMT,而不是特定于时区.
如果我输出time.gmtime,我会得到正确的时间(应该保存):
2011-10-26 11:30:00 UTC (correct)
Run Code Online (Sandbox Code Playgroud)
有任何想法如何保存GMT日期,以便它实际保存GMT日期?
我在我的模型中定义了以下范围:
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集和即将到来的集合的结合.
有任何想法吗?谢谢.
我正在使用Mongoid(v3)来访问MongoDB,并希望执行此操作:
db.sessionlogs.update(
{sessionid: '12345'}, /* selection criteria */
{'$push':{rows: "new set of data"}}, /* modification */
true /* upsert */
);
Run Code Online (Sandbox Code Playgroud)
这在mongo shell中工作正常.它也正是我想要的,因为它是一个单一的原子操作,对我来说很重要,因为我会调用它.我不想做两个操作 - 获取然后更新.我通过mongoid尝试了很多东西,但无法让它工作.
如何将MongoID取出并将此命令发送到MongoDB?我猜测在Moped级别有一些方法可以做到这一点,但该库的文档基本上不存在.
我正在使用带有rails和MongoDB的ember-data,并且对于在MongoDB中存储ID的方式存在问题 - 在_id字段中.
Ember-data将使用id作为ID的默认字段,所以我试图像这样覆盖它:
App.User = DS.Model.extend
primaryKey: "_id"
name: DS.attr "string"
image: DS.attr "string"
Run Code Online (Sandbox Code Playgroud)
这似乎在大部分时间都有效,但在某些情况下,我从ember说得到例外:
未捕获错误:断言失败:您的服务器返回带有密钥_id的哈希,但您没有映射
我怀疑这可能是ember-data中的一个错误,因为它仍处于开发阶段,但是我试图找到一种方法来将_id映射到服务器端的id中?我正在使用mongoid来进行mongo映射.
刚刚更新到最新版本的mongoid(3.1.0)和sidekiq(2.7.2)
现在我收到错误:failed with error 10068: "invalid operator: $oid"
从查看代码我看到一个类似的选择:@selector={"_id"=>{"$oid"=>"[some id]"}}
这里可能出错?
如果首先使用belongs_to和has_many关联构建模型,然后意识到他们需要转移到embedded_in和embeds_many关联,那么如何在不使数千条记录失效的情况下执行此操作?需要以某种方式迁移它们.
我理解这不是一个编程问题,我无法找到一个非常清晰和描述性的解决方案.
在运行Sidekiq时,我看到大量的工作失败,这些工作都与我的Mongo数据库的连接问题有关.我压力测试机器有很多负载,所以我排队了18,000多个工作,失败时重试5秒.一些工作(我猜测那些能够成功检索连接线程的工作者)工作得很好.然后我有很多其他人有这样的错误:
2013-12-26T19:25:35Z 13447 TID-m2biw WARN: {"retry"=>true, "queue"=>"default",
"class"=>"ParseFileWorker", "args"=>[{"filename"=>"/opt/media/AE-67452_36.XML"}],
"jid"=>"5d6c48fa94ed8c8da1b226fc", "enqueued_at"=>1388084903.6113343,
"error_message"=>"Could not connect to a primary node for replica set #
<Moped::Cluster:44552140 @seeds=[<Moped::Node resolved_address=\"127.0.0.1:27017\">]>",
"error_class"=>"Moped::Errors::ConnectionFailure", "failed_at"=>"2013-12-26T19:16:25Z",
"retry_count"=>3, "retried_at"=>2013-12-26 19:25:35 UTC}
Run Code Online (Sandbox Code Playgroud)
还有超时错误,如下所示:
Timeout::Error: Waited 0.5 sec
Run Code Online (Sandbox Code Playgroud)
注意,我正在运行Rails 4,并在https://github.com/mongoid/mongoid中从主分支中检出Mongoid代码.根据我的阅读,以前版本的Mongoid需要在Sidekiq作业完成处理时明确关闭连接.Mongoid 4应该自动完成.我无法确认它是否正在这样做.当作业排队太快,连接不可用或超时时,问题似乎是两倍.一些工人成功地打开了连接.有些工作必须等到重试解析.
我不完全确定这里最好的解决方案是什么.如果我慢慢排队工作,一切正常.不幸的是,这不是应用程序在现实世界中的运作方式.
我如何确保我的sidekiq作业获得完成工作所需的Moped/Mongoid连接,或者等待其中一个可用于处理?由于这个问题,看到了大量的例外/错误.下面是常见错误的堆栈跟踪:
2013-12-26T20:56:58Z 1317 TID-i70ms ParseFileWorker JID-0fc375d8fd9707e7d3ec3238 INFO: fail: 1.507 sec
2013-12-26T20:56:58Z 1317 TID-i70ms WARN: {"retry"=>true, "queue"=>"default", "class"=>"ParseFileWorker", "args"=>[{"filename"=>"/opt/media/AC-19269_287.XML"}], "jid"=>"0fc375d8fd9707e7d3ec3238", "enqueued_at"=>1388091410.0421875, "error_message"=>"Waited 0.5 sec", "error_class"=>"Timeout::Error", "failed_at"=>2013-12-26 20:56:58 UTC, "retry_count"=>0}
2013-12-26T20:56:58Z 1317 TID-i70ms WARN: Waited 0.5 sec
2013-12-26T20:56:58Z 1317 TID-i70ms …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过mongoid.yml进行身份验证,但我想要在admin数据库中进行身份验证.如果我尝试在数据库字段中指定admin数据库,它会将所有集合放在那里,我不希望这样.
有没有办法将我的数据库字段设置为我想要的数据库,但是指定一个单独的身份验证数据库?这是我的mongoid.yml文件
development:
sessions:
default:
database: XC_DEV
hosts:
- IP:PORT
username: user
password: password
Run Code Online (Sandbox Code Playgroud) 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