0

I am quite new in JS, mongoDB and mongoose.

I would like to try to copy some documents field to another document. After that i would like to delete the original document.

Here is my mongoDB collection:

    [
// twitter account

{ "_id"     : "585bbba59659501d68f997ec", 
  "logIn"   : "twitter" , 
  "pollIds" : { "local" : [ ] , "facebook" : [ ] , "twitter" : [ ]} ,
  "twitter" : { "username"      : "xxxxx" , 
                "displayName"   : "xxxx yyyyy" , 
                "token"         : "" , //place for copied twitter token
                "id"            : "99999999999999999"} ,
  "__v" : 0},

  //facebook account connected with twitter and local

{ "_id"     : "585bbba99659501d68f997ed",
  "logIn"   : "facebook" , 
  "pollIds" : { "local" : [ ] , "facebook" : [ ] , "twitter" : [ ]} , 

  "twitter" : { "username"      : "xxxxx" ,
                "displayName"   : "xxxx yyyyy" , 
                "token"         : "tokentokentokentoken" , //token what i want to copy 
                "id"            : "99999999999999999" , 
                "dbId"          : "585bbba59659501d68f997ec"} ,

  "facebook": { "email"         : "xx@xx.com" ,
                 "displayName"  : "xxx  yyy" , 
                 "token"        : "facebooktoken" , 
                 "id"           : "8888888888888888"} , 

  "local"   : {  "password"     : "local user password" , 
                 "email"        : "ggg@ggg.com" , //email what i want to copy
                 "dbId"         : "585bbbc29659501d68f997ee"},
  "__v" : 0}

//local account

{ "_id"     : "585bbbc29659501d68f997ee", 
  "logIn"   : "local" , 
  "pollIds" : { "local" : [ ] , "facebook" : [ ] , "twitter" : [ ]} ,
  "local"   : { "password"  : "local password" ,
                "email"     : ""} , place for copied local email
  "__v" : 0}

]

What I try to achieve is disconnect TWITTER and LOCAL accounts from FACEBOOK account. Here is how I try to do this :

  1. from FACEBOOK account copy TWITTER token to the TWITTER account
  2. from FACEBOOK account copy LOCAL email to the LOCAL account
  3. from FACEBOOK account remove TWITTER and LOCAL account assigning them undefined

    // local = undefined // // twitter = undefined //

My problem is: after that I remove LOCAL and TWITTER accounts, the copied fields disappears as well. I think I just copied its reference and not its value. If that is the situation how i can make this work ?

I hope my question doesn't seem messy. This is my first post here. Thank for your answer.

Otti
  • 374
  • 5
  • 9
  • [This post](http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript/5344074#5344074) may help you: `var clone = JSON.parse(JSON.stringify(obj));` – pistou Dec 22 '16 at 13:59

2 Answers2

0

So the thing with Objects in Node is that when you "copy" them (i.e. var x = y) you are actually creating a pointer to the original object. What you likely want to do is the following:

var newObj = {};
Object.assign(newObj, originalObj);
delete originalObj['somekey'];

So what that does is it takes the original object, makes a deep copy of it into a new object (2 separate memory locations), and then at the end I'm using delete to remove keys (essentially setting them undefined).

Reference this for more info on Object.assign: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

firrae
  • 171
  • 1
  • 13
0

Just follow the code !

let cloneObject = JSON.parse(JSON.stringify(object1));