Q: MongoQuery to update document using addition I have the following document in student collection:
{
"uid": 1,
"eng": 70
}

Now I want to add 10 into eng field and want result 80. to do this I am using following query:
db.getCollection('student').aggregate([{$match:{uid:1}},{$set:{eng:{$sum:10}}}])

but it is not working. SO how can add any number in the field to the required output? is any addition query in MongoDB. help me here

A: SOLUTION #1: Set sum to the same field eng.
db.student.aggregate([
    { $match: { uid: 1 } },
    {
        $set: {
            eng: { $add: ["$eng", 10] }        // $sum: ["$eng", 10] Also works;)
        }
    }
])

Output:
{
    "_id" : ObjectId("6065f94abb72032a689ed61d"),
    "uid" : 1,
    "eng" : 80
}

SOLUTION #2: Set sum to a different field result.

*

*Using $addFields add result filed.

*Using $add add 10 to eng and store it in result.

db.student.aggregate([
    { $match: { uid: 1 } }, 
    { 
        $addFields: { 
            result: { $add: ["$eng", 10] }    
        } 
    }
])

Output:
{
    "_id" : ObjectId("6065f94abb72032a689ed61d"),
    "uid" : 1,
    "eng" : 70,
    "result" : 80
}


A: I suggest using the $inc operator here:
db.getCollection('student').update(
    { uid: 1 },
    { $inc: { eng: 10 } }
)

