Created
November 19, 2012 22:03
-
-
Save Tug/4114350 to your computer and use it in GitHub Desktop.
ERROR File not found
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; | |
} | |
for(var i=0;i<10;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); | |
}); | |
}); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output is :
Connected to MongoDB
ERROR File not found
ERROR File not found
ERROR File not found
ERROR File not found
ERROR File not found
ERROR File not found
ERROR File not found
ERROR File not found
ERROR File not found
ERROR File not found