Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb - Add value to the existing integer field in a query

Tags:

mongodb

What i am doing is -

db.getCollection('campaigns').update(
{'_id' : ObjectId("57aea88f2f1321710f7373f8")},
{ $set : {
        _a :  'this._a' + 1000
    }
}
);

I tried this also :

db.getCollection('campaigns').update(
{'_id' : ObjectId("57aea88f2f1321710f7373f8")},
{ $set : {
        _a :  this._a + 1000
    }
}
);

What i am getting is _a = nan.

Please help me in this. I am very new to MongoDb. What I need is add value in _a field with 1000.

like image 308
Katti Avatar asked Aug 26 '16 07:08

Katti


People also ask

How do I increment a field in MongoDB?

The $inc operator increments a field by a specified value and has the following form: { $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } } To specify a <field> in an embedded document or in an array, use dot notation.

Is it possible to update MongoDB field using value of another field?

Starting from MongoDB 4.2 you can perform Updates with an Aggregation Pipeline. An aggregation pipeline enables more expressive updates including calculated fields and references to other field values in the same document.

How do you value a field in MongoDB?

You can extract the value of a field by appending that field's name to your query when using findOne() .


1 Answers

db.campaigns.update(
   { '_id' : ObjectId("57aea88f2f1321710f7373f8") },
   { $inc: { _a: 1000 } });
like image 172
Vora Ankit Avatar answered Nov 16 '22 04:11

Vora Ankit