0
coll.save({"_id" => "test", "1" => "a"}) #=> {"_id"=>"test", "1"=>"a"}
coll.update({"_id" => "test"}, {"$set"=>{"1.2" => "b"}}) #=> {"_id"=>"test", "1"=>"a"}

I was expecting that last line would create a new node: {"_id"=>"test", "1"=>{"2"=>"b"}} or {"_id"=>"test", "1"=>["a",{"2" => "b"}]}

Is it possible to have it create a node that doesn't exist? and to unshift into an array if there's already a key:value pair?

one more:

coll.save({"_id" => "test", "1" => ["a"]}) #=> {"_id"=>"test", "1"=>["a"]}
coll.update({"_id" => "test"}, {"$set"=>{"1" => ["b"]}}) #=> {"_id"=>"test", "1"=>["b"]}

Is it possible to unshift values into arrays, without overwriting them? thus making above: #=>{"_id"=>"test", "1"=>["a","b"]}

I say this because I have large arrays, and there's a performance overhead if I download the array, and unshift it on Ruby's side.

And with the first question, the documents in the collection have a variety of shapes, and building a when:case method in ruby would be a lot of overhead / work for all the possibilities.

Ruby unshift: http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-unshift

::edit::

$db.serverStatus().version
2.0.2

$gem list
mongo (1.6.2, 1.6.1, 1.5.2)

Exact code:

require 'pp'
require 'mongo'
coll = Mongo::Connection.new.db("test").collection("test")
coll.save({"_id" => "test", "1" => "a"})
pp coll.find_one("_id"=>"test") #=> {"_id"=>"test", "1"=>"a"}
coll.update({"_id" => "test"}, {"$set"=>{"1.2" => "b"}})
#Expect, but does not give: {"_id"=>"test", "1"=>{"2"=>"b"}}
pp coll.find_one("_id"=>"test") #=> {"_id"=>"test", "1"=>"a"} 
Mr. Demetrius Michael
  • 2,326
  • 5
  • 28
  • 40
  • I'm unable to reproduce your first case shown above; what version of MongoDB are you using? What version of the ruby driver? – dcrosta May 25 '12 at 14:57
  • latest, coll is short for collection, sorry for not being specific. `coll = Mongo::Connection.new.db("test")` I'll check out your answer soon, thanks for replying. And the output isn't exactly the output either. I just commented what was stored into the DB, rather than the output from save / update, which is minimal. – Mr. Demetrius Michael May 25 '12 at 15:34
  • Can you show the exact code you're using? Also, you can find out the version of MongoDB by doing `db.serverStatus().version` in the mongo shell, and the version of the ruby driver by doing `gem list` in your terminal. – dcrosta May 25 '12 at 17:24
  • Updated. Do you reproduce different results? – Mr. Demetrius Michael May 25 '12 at 17:58
  • I get an error when I try to run that (with the addition of `:safe => true`, which causes the driver to send a bundled `getLastError` command). – dcrosta May 25 '12 at 20:21

1 Answers1

0

MongoDB doesn't have a prepend operator, but it does have two operators which append to arrays atomically: $push, which unconditionally appends to the array, and $addToSet, which only adds to an array only if it does not already contain the element being added.

dcrosta
  • 26,009
  • 8
  • 71
  • 83