Arnab Bhadury
Arnab Bhadury

Reputation: 108

Is there any way to replace an entire array in MongoDB?

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.

Upvotes: 0

Views: 1762

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311875

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]}})

Upvotes: 2

Related Questions