node.js - MongoDb: How to merge contents of two documents in mongoDB? -


i allow user skip login or login using fb. when user logs in create user in user collection using fb profile id , when skips still create user using unique client id. in client app, user can bookmark articles , sync server. user can login using fb @ time later. once user logs in using fb have merge user created using fb , user created using client id stored data maps perfectly.

what have tried:

consider user skipped login , create user in user collection. user logs in using fb in between life time of app , search fb id in collection. if id not exist update skipped user document fb id. if exist merge data fb user document skipped user document , delete fb user document.

another method:

i don't sync bookmark server until user logs in. once logs in sync data @ once.

what efficient way of doing kind of operation?

if understand correctly, need find document same clientid, , add facebook user document.

you can mongo update command, using $set merges new document existing mongo document https://docs.mongodb.com/manual/reference/operator/update/set/

so code this

db.collection("users").update(    // first parameter mongo query match based on clientid    { clientid: 100 },    // second parameter document merged using $set    { $set: facebookdocument }) 

the result of existing document merged properties of facebookdocument


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -