How to sum up all the employee salaries and update it to one attribute in the array embedded doc in mongodb -


i want sum emp_salary = (9000)2000+3000+4000 , i'm trying update value 9000 total_employee_salary attribute.how can in mongo shell.can please me out regarding ...

{         "_id" : objectid("571898dbc000041fe0b921eb"),         "organization" : "abc",         "total_employees" : 10,         "total_employees_salary" : 0,          "employees" : [                 {                         "emp_name" : "vijay",                         "emp_salary" : 2000,                  },                 {                         "emp_name" : "vishnu",                         "emp_salary" : 3000,                  },                 {                         "emp_name" : "vishal",                         "emp_salary" : 4000,                  }         ] } 

if doing in bulk collection, best way iterate .bulkwrite() write back:

var ops = []; db.collection.find().foreach(function(doc) {     ops.push({         "updateone": {             "filter": { "_id": doc._id },             "update": {                 "$set": {                     "total_employee_salary": array.sum(doc.employees.map(function(emp) {                         return emp.emp_salary                     }))                 }             }         }     });      if ( ops.length == 1000 ) {         db.collection.bulkwrite(ops);         ops = [];     } })  if ( ops.length > 0 ) {     db.collection.bulkwrite(ops); } 

for "super safe" code though, should using $inc on iteration of each array element instead:

var ops = [];  db.collection.find().foreach(function(doc) {     doc.employees.foreach(function(emp) {         ops.push({             "updateone": {                 "filter": { "_id": doc._id },                 "update": {                      "$inc": {                          "total_employee_salary": emp.emp_salary                     }                 }             }         });          if ( ops.length == 1000 ) {             db.collection.bulkwrite(ops);             ops = [];         }     }) });  if ( ops.length > 0 ) {     db.collection.bulkwrite(ops); } 

in earlier shell releases using "bulk" operations builder directly:

var bulk = db.collection.initializeorderedbulkop(),     count = 0;  db.collection.find().foreach(function(doc) {     bulk.find({ "_id": doc._id }).updateone({         "$set": {              "total_employee_salary": array.sum(doc.employees.map(function(emp) {                 return emp.emp_salary             }))         }     });     count++;      if ( count % 1000 == 0 ) {         bulk.execute();         bulk = db.collection.initializeorderedbulkop();     } })  if ( count % 1000 != 0 ) {     bulk.execute(); } 

but should doing in instances updating mongodb-shell package on system, regardless of server version used. modern shell should updated modern api version programming language of choice.

you need iterate documents anyway in order update each one, might sum content array reading each document.

just trivia sake, in modern mongodb releases $sum operator works both accumulator adding items in array. can this:

db.collection.aggregate([     { "$project": {         "total_employee_salary": {            "$sum": "$employees.emp_salary"         }     }} ]) 

and give total of array in each document.

in earlier versions mongodb 3.2 though, need $unwind array , $group instead:

db.collection.aggregate([     { "$unwind": "$employees" },     { "$group": {         "_id": "$_id",         "total_employee_salary": { "$sum": "$employees.emp_salary" }     }}  ]) 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -