更新数组中的元素

时间:2015-05-02 22:44:23

标签: mongodb

假设我有这个对象:

{town_id: 13, houses_data: [
    {house_id: 5, price: 32, description: "thats a house"},
    {house_id: 2, price: 12, description: "thats a house"}
   ]
 }

我想更新id为5的房子,以及#34; <&b>出售&#34;

{town_id: 13, houses_data: [
    {house_id: 5, price: 32, description: "sold"},
    {house_id: 2, price: 12, description: "thats a house"}
   ]
 }

我尝试了什么:

 town1 = town.findOne({town_id: 13});

获取houses_data:

 twon1.houses_data

并尝试仅更新house_data id = 5

 twon1.houses_data.find({house_id: 5}).update(description: "sold");

但是我收到此错误消息:

 [object Object],[object Object] has no method 'find'

我错了什么?感谢

1 个答案:

答案 0 :(得分:2)

您可以使用$更新与给定查询匹配的第一个嵌入文档:

{{1}}