Lets say I have a document Person
, and I wish to add Tags
to this document.
So I would like a document to look like
{ "_id" : 5,
"tags" : ["Music", "Baseball", "Skiing"]
}
Everytime a person chooses an interest, I would like to append it to the tags
list.
Here is my attempt:
require 'mongo'
client = Mongo::Connection.new
db = client['some_app']
coll = db['persons']
coll.update({_id: 5}, {"$push" => {tags: "PS4"}}, {upsert: true})
If I run this I get an exception:
/usr/local/rvm/gems/ruby-2.1.2/gems/bson-1.10.2/lib/bson/bson_c.rb:20:in `serialize': key $push must not start with '$' (BSON::InvalidKeyName)
What am I doing wrong?