Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment existing value in MongoDB

Tags:

I am using Stitch platform by MongoDB. I want to store a value and a count associated with that value in the database. Now the value may not be present for the first time, so I would like to insert the value with count = 1. I can use update() to update the existing value of count using $inc or I can use upsert() to add the value in the database. Now, the thing is, I have a map of my values and the count, and I want to insert(update/upsert) all at once. I don't want to put a load on the network. I was using insertMany() to insert the map at once but it clearly doesn't updates the value.

So is it possible to do so?

P.S. I am using javascript for the same.

like image 509
Nikhil Wagh Avatar asked Dec 28 '17 11:12

Nikhil Wagh


People also ask

What is the use of $Inc in MongoDB?

The $inc operator accepts positive and negative values. If the field does not exist, $inc creates the field and sets the field to the specified value. Use of the $inc operator on a field with a null value will generate an error. $inc is an atomic operation within a single document.

How does Upsert work in MongoDB?

Here in MongoDB, the upsert option is a Boolean value. Suppose the value is true and the documents match the specified query filter. In that case, the applied update operation will update the documents. If the value is true and no documents match the condition, this option inserts a new document into the collection.

What is $Set in MongoDB?

$set outputs documents that contain all existing fields from the input documents and newly added fields. The $set stage is an alias for $addFields . Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

What is bulkWrite in MongoDB?

MongoDB allows applications to determine the acceptable level of acknowledgement required for bulk write operations. The db. collection. bulkWrite() method provides the ability to perform bulk insert, update, and delete operations.


1 Answers

According to MongoDb 3.6:

db.collection.update(query, update, options)

Modifies an existing document or documents in a collection. The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update parameter.

The meaning is that you can upsert multiple documents using update.

First you should create array from your map that contains only the value.

const arrayOfValues = ['value_01', 'values_02'];

Then you should use the upsert + multi options on the update method:

db.foo.update({value: { $in: arrayOfValues}}, {$inc: {count:1}}, { upsert: true, multi: true });

Test output:

> db.createCollection("test");
{ "ok" : 1 }
> db.test.insertMany([{value: "a"}, {value: "b"}, {value: "c"}];
... );
2017-12-31T12:12:18.040+0200 E QUERY    [thread1] SyntaxError: missing ) after argument list @(shell):1:61
> db.test.insertMany([{value: "a"}, {value: "b"}, {value: "c"}]);
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5a48b8061b98cc5ac252e435"),
        ObjectId("5a48b8061b98cc5ac252e436"),
        ObjectId("5a48b8061b98cc5ac252e437")
    ]
}
> db.test.find();
{ "_id" : ObjectId("5a48b8061b98cc5ac252e435"), "value" : "a" }
{ "_id" : ObjectId("5a48b8061b98cc5ac252e436"), "value" : "b" }
{ "_id" : ObjectId("5a48b8061b98cc5ac252e437"), "value" : "c" }
> db.test.update({value: { $in: ["a", "b", "c"]}}, {$inc: {count:1}}, { upsert: true, multi: true });
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.test.find();
{ "_id" : ObjectId("5a48b8061b98cc5ac252e435"), "value" : "a", "count" : 1 }
{ "_id" : ObjectId("5a48b8061b98cc5ac252e436"), "value" : "b", "count" : 1 }
{ "_id" : ObjectId("5a48b8061b98cc5ac252e437"), "value" : "c", "count" : 1 }
> db.test.update({value: { $in: ["a", "b", "c"]}}, {$inc: {count:1}}, { upsert: true, multi: true });
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.test.find();
{ "_id" : ObjectId("5a48b8061b98cc5ac252e435"), "value" : "a", "count" : 2 }
{ "_id" : ObjectId("5a48b8061b98cc5ac252e436"), "value" : "b", "count" : 2 }
{ "_id" : ObjectId("5a48b8061b98cc5ac252e437"), "value" : "c", "count" : 2 }
> db.test.update({value: { $in: ["a", "b", "c"]}}, {$inc: {count:1}}, { upsert: true, multi: true });
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

Hope it was helpful :)

like image 138
Yochai Akoka Avatar answered Sep 22 '22 12:09

Yochai Akoka