Skip to content

Instantly share code, notes, and snippets.

@zamnuts
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zamnuts/fb5c569c17a9f0806b9b to your computer and use it in GitHub Desktop.
Save zamnuts/fb5c569c17a9f0806b9b to your computer and use it in GitHub Desktop.
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