Last active
August 29, 2015 14:11
-
-
Save zamnuts/fb5c569c17a9f0806b9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MongoClient = require('mongodb').MongoClient; | |
MongoClient.connect('mongodb://localhost/fromdb',function(err,dbLocal) { | |
if ( err ) throw err; | |
MongoClient.connect('mongodb://remotehost/todb',function(err,dbRemote) { | |
if ( err ) throw err; | |
var collectionLocal = dbLocal.collection('fromcollection'), | |
collectionRemote = dbRemote.collection('tocollection'), | |
queryLocal = {field:'value'}; | |
collectionLocal.find(queryLocal).each(function(err,docLocal) { | |
if ( err ) throw err; | |
// transform docLocal | |
var queryRemote = {_id:docLocal._id}; | |
collectionRemote.findAndModify(queryRemote,[['_id',1]],docLocal,{upsert:true,new:true},function(err,docRemote) { | |
if ( err ) throw err; | |
console.log('upserted %s : %s',docLocal._id,docRemote._id); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment