我在MongoDB中有一个文档,它包含一些元信息,如更新日期和_id,以及一组地址。
{
"_id": {
"$oid": "4e73a30466ca1a1f56000001"
},
"updated": 1316215062,
"address": [
{
"street": "Rotenturmstrasse 8",
"postcode": "1020",
"phone": "Vienna",
"altitude": -1,
"geolocation": [
"11.367464",
"47.204876"
]
}
]
}
现在可能有多个地址。我正在为一个地址创建一个对象,需要更新并将其保存到数据库中。这就是要插入的新对象的样子:
new_object = {
:_id=>BSON::ObjectId('4e73a30466ca1a1f56000001'),
:updated=>1316215099,
:address=>[
nil,
nil,
{
:street=>"Reumannplatz 8",
:postcode=>"1020",
:phone=>"Vienna",
:altitude=>-1,
:geolocation=>[
"12.367464",
"48.204876"
]
}
]
}
在致电db.venues.save(new_object)
后,我希望文档最终看起来像是:
{
:_id=>BSON::ObjectId('4e73a30466ca1a1f56000001'),
:updated=>1316215099,
:address=>[ {
"street": "Rotenturmstrasse 8",
"postcode": "1020",
"phone": "Vienna",
"altitude": -1,
"geolocation": [
"11.367464",
"47.204876"
]
},
nil,
{
:street=>"Reumannplatz 8",
:postcode=>"1020",
:phone=>"Vienna",
:altitude=>-1,
:geolocation=>[
"12.367464",
"48.204876"
]
}
]
}
相反,它会覆盖整个数组并最终结束:
{
:_id=>BSON::ObjectId('4e73a30466ca1a1f56000001'),
:updated=>1316215099,
:address=>[
nil,
nil,
{
:street=>"Reumannplatz 8",
:postcode=>"1020",
:phone=>"Vienna",
:altitude=>-1,
:geolocation=>[
"12.367464",
"48.204876"
]
}
]
}
这样做的方法是什么?我试图避免多个查询。如果数据库中的地址数组是哈希值,它会使事情变得更容易吗?