0

So, I have a problem in MongoDB... I have stored some data in MongoDB and it basically looks like:

{
    _id: 1,
    name: "aa",
    importance: [0.5, 0.25, 0.25]
}

Where this importance attribute is an array that I will have to keep updating, e.g., after getting some data, it should be updated to [0.80, 0.10, 0.10]...

In this toy problem, I don't really understand how to replace a full array. Do I have to do it elementwise somehow? If so, it is not a tractable solution in my case, because the number of elements in my array reach above 1000.

1
  • Have you looked this $each Commented Jun 8, 2015 at 17:38

1 Answer 1

2

You can replace the entire importance array in an update by using $set:

db.test.update({_id: 1}, {$set: {importance: [0.80, 0.10, 0.10]}})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks sooo much! I was reading the docs and still somehow missed out on $set.

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.