Created
November 19, 2012 22:57
-
-
Save Tug/4114656 to your computer and use it in GitHub Desktop.
success! (except for last one)
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 mongodb = require('mongodb') | |
, Db = mongodb.Db | |
, Server = mongodb.Server | |
, ReplSetServers = mongodb.ReplSetServers; | |
var mode = process.argv.slice(2)[0] || 'server'; | |
var mongoConfig = { | |
replset : { | |
servers : [ | |
{ host: 'localhost', port: 27017, options : { auto_reconnect: true, poolSize: 1 } } | |
, { host: 'localhost', port: 27018, options : { auto_reconnect: true, poolSize: 1 } } | |
, { host: 'localhost', port: 27019, options : { auto_reconnect: true, poolSize: 1 } } | |
] | |
, options : { | |
rs_name : 'rs0' | |
, read_secondary : true | |
} | |
} | |
, server : { host: 'localhost', port: 27017, options : { auto_reconnect: true } } | |
, options : { | |
safe : { w: 'all', wtimeout: 5000 } | |
} | |
, db : 'test-replset' | |
}; | |
function createServer(serverInfo) { | |
return new Server(serverInfo.host, serverInfo.port, serverInfo.options); | |
} | |
function createReplicaSet(replsetConfig) { | |
return new ReplSetServers(replsetConfig.servers.map(createServer), replsetConfig.options); | |
} | |
function connect(callback) { | |
var replset, server; | |
if(mode == 'replset') { | |
replset = createReplicaSet(mongoConfig.replset); | |
} else { | |
server = createServer(mongoConfig.server); | |
} | |
if(mongoConfig.options.safe && mongoConfig.options.safe.w == 'all' ) { | |
mongoConfig.options.safe.w = (replset) ? replset.servers.length : 1; | |
} | |
var client = new Db( mongoConfig.db | |
, replset || server | |
, {safe:true } ); | |
client.open(function(err, db) { | |
if(err) { | |
console.log(err); | |
return; | |
} | |
console.log(err || "Connected to MongoDB"); | |
callback(null, client); | |
}); | |
} | |
function success(doc) { | |
console.log("success!", doc); | |
} | |
function error(err) { | |
console.log("ERROR",err.message); | |
} | |
connect(function(err, db) { | |
db.collection('files', function(err, files) { | |
files.remove({}, function(err) { | |
if(err) { | |
error(err || new Error('Error emptying collection')); | |
return; | |
} | |
function insertAndFind(i) { | |
files.insert({servername: "hello"+i, originalname: "world"+i}, function(err, docs) { | |
if(err || !docs) { | |
error(err || new Error('Error inserting file')); | |
return; | |
} | |
files.findOne({servername: "hello"+i}, function(err, doc) { | |
if(err || !doc) { | |
error(err || new Error('File not found')); | |
return; | |
} | |
success(doc); | |
}); | |
}); | |
if(i-->0) { | |
process.nextTick(function() { | |
insertAndFind(i); | |
}); | |
} | |
} | |
insertAndFind(10); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output is :
Connected to MongoDB
success! { servername: 'hello9',
originalname: 'world9',
_id: 50aab8976fc9921727000002 }
success! { servername: 'hello8',
originalname: 'world8',
_id: 50aab8976fc9921727000003 }
success! { servername: 'hello7',
originalname: 'world7',
_id: 50aab8976fc9921727000004 }
success! { servername: 'hello6',
originalname: 'world6',
_id: 50aab8976fc9921727000005 }
success! { servername: 'hello3',
originalname: 'world3',
_id: 50aab8976fc9921727000008 }
success! { servername: 'hello4',
originalname: 'world4',
_id: 50aab8976fc9921727000007 }
success! { servername: 'hello5',
originalname: 'world5',
_id: 50aab8976fc9921727000006 }
success! { servername: 'hello0',
originalname: 'world0',
_id: 50aab8976fc992172700000b }
success! { servername: 'hello2',
originalname: 'world2',
_id: 50aab8976fc9921727000009 }
success! { servername: 'hello1',
originalname: 'world1',
_id: 50aab8976fc992172700000a }
ERROR File not found