user556698
user556698

Reputation: 65

MongoDB : Multi Update with where condition

Requirement: Get All documents where point < 90 and increment 'point' by +20 Data:

 { "_id" : 1, "student" : 1, "type" : "exam", "point" : 115 }
    { "_id" : 2, "student" : 2, "type" : "exam", "point" : 85 }
    { "_id" : 3, "student" : 3, "type" : "exam", "point" : 115 }

Query tried : 
// query below 

db.points.update({},{$inc:{point:20},point:{lt:110}},{multi:true})

On executing : fn[0] == '$'is seen and query has not affected any changes.

Upvotes: 1

Views: 1633

Answers (2)

Michal V&#237;ch
Michal V&#237;ch

Reputation: 1

There is no where condition in your update

db.points.update({ point : { $lt: 90 } },{ $inc: { point:20 } },{multi:true})

point : { $lt: 90 } selects only documents where a value in point is lower then 90 and increases by 20

Upvotes: 0

Parvin Gasimzade
Parvin Gasimzade

Reputation: 26012

You can write query as follows :

db.points.update({point:{$lt:110}},{$inc:{point:20}},false,true)

Upvotes: 1

Related Questions