1

Lets say I have this Object:

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

And I want to update the desription of house with id 5 to "sold":

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

What I tried:

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

Get the houses_data:

 twon1.houses_data

And tried to update only the house_data where id = 5

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

But I get this error message:

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

What do I wrong? Thanks

1 Answer 1

2

You might use $ to update the first embedded document matching the given query:

db.test.town.update({town_id: 13, "houses_data.house_id":5},
                    {$set: { "houses_data.$.description": "sold"}})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.