1

In a document I have an object called farms and inside this object I would like to update key-value pairs in the following mannner. How would I do this?

$set:{"farms[${farm._id}]":{name:"a-farm", size:100}

the result would look like this(assuming farm._id = 12345)

farms:{"12345": {name"a-farm", size: 100}}

1 Answer 1

2

You can use $set and build your key dynamically using dot notation, try:

var farm = { _id: 12345 }
var path = "farms." + farm._id;
var documentId = ... // your document id

db.col.update({ _id: documentId  }, { $set: { [path]: { name:"a-farm", size:100 } }  })
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.