node.js - Merge lodash array nodejs mongodb -


i want merge collection updating number field , array.

this schema mongodb :

{    url: string,    email: [string],         vote: number } 

node.js code :

like = _.merge(like, req.body); like.save(function (err) {     if (err) { return handleerror(res, err); } }); 

this code updates vote field incrementing value, replaces existing email whereas want push email array.

i want increment vote value, , push email array, ["a@gmail.com", "b@gmail.com",... ] etc.

i don't know how that.

use _.assign() instead works in way each property in source, copies value as-is destination. if property values objects, there no recursive traversal of properties. entire object taken source , set in destination. following examples demonstrate differences between _.assign() , _.merge():

var dest = {   p: { x: 10, y: 20}, };  var src = {   p: { x: 20, z: 30}, };  console.log(_.merge(dest, src)); 

output

[object object] {   p: [object object] {     x: 20,     y: 20,     z: 30   } }  console.log(_.assign(dest, src)); 

output

[object object] {   p: [object object] {     x: 20,     z: 30   } } 

so final code should like:

like = _.assign(like, req.body); like.save(function (err) {     if (err) { return handleerror(res, err); } }); 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -