English 中文(简体)
采用警戒办法,推动对Ruby驾驶员的阵列选择
原标题:Using upsert with push to an array option on Ruby driver
  • 时间:2012-01-13 16:22:05
  •  标签:
  • ruby
  • mongodb

I m trying to do an upsert with ruby driver to mongodb. If the row exist I wish to push new data to and array, else create new document with one item in the array.

当我执政时,我看着:

db.events.update( { "_id" : ObjectId("4f0ef9171d41c85a1b000001")}, 
{ $push : { "events" : { "field_a" : 1 , "field_b" : "2"}}}, true)

并且是行之有效的。

When I run it on ruby it looks like that:

@col_events.update( { "_id" => BSON::ObjectId.from_string("4f0ef9171d41c85a1b000001")}, 
{ :$push => { "events" => { "field_a" => 1 , "field_b" => "2"}}}, :$upsert=>true)

但它没有工作。 我不会错过,但我看不到新行。

感谢帮助了解我做错做的事情。

最佳回答

因此,有几个问题。

  1. In Ruby, the command should be :upsert=>true. Note that there is not $. The docs for this are here.
  2. You are not running the query with :safe=>true. This means that some exceptions will not fire. So you could be causing an exception on the server, but you are not waiting for the server to acknowledge the write.
问题回答

仅添加一些关于盖茨基金会的出色答案:

require  rubygems 
require  mongo 

@col_events = Mongo::Connection.new()[ test ][ events ]

#safemode enabled
@col_events.update(
  { "_id" => BSON::ObjectId.from_string("4f0ef9171d41c85a1b000001")},
  { "$push" => { "events" => { "field_a" => 1, "field_b" => "2"}}},
  :upsert => true, :safe => true
)




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签