I'm trying to update a hash in a MongoDB doc with a simple value but it store the value in an array. I use the ruby driver for mongo
Code will explain better because my english is bad.
What I have :
{
'id' : ...
'stream' : {
"1406481985(a timestamp)" : 35603
}
}
What I want :
{
'id' : ...
'stream' : {
"1406481985" : 35603,
"1406481990" : 15000
}
}
What I get :
{
'id' : ...
'stream' : {
"1406481985" : 35603,
"1406481990" : [
15000
]
}
}
How did I get there :
views = 15000
time = Time.now
coll.find_and_modify({
query: {:id => id},
update: {'$push' => {"stream.#{time}" => views}},
})
I've already tried with Updating nested document in MongoDB and I can't see what I do wrong
Daniël Knippers is correct — using $set
should work. I also notice you are using id
instead of '_id`. Perhaps a typo?
edit: note that it's usually not recommended to have dynamic key values as they are difficult to index and query. Consider a stream
structure of hashes inside an array:
{
'_id' : ...
'stream' : [
{'time' : 1406481985, 'views': 35603},
{'time' : 1406481990, 'views': 15000}
]
}
Now you can easily query the time
and views
fields.