Javascript closures + async i/o are biting you.
for(var i=0;i<10;i++) {
var file = new FileModel({servername: "hello"+i, originalname: "world"+i});
file.save(function(serr) {
Here's whats happening, all files are being created and saved as you intend and `i` is incremented each time.
file.save(function(serr) {
FileModel.findOne({servername: "hello10"}
Firstly, you should handle any potential `serr` returned.
Second, and more importantly, by the time any save callback executes, the `for()` loop has already exited and `i` has been incremented again, which leaves it set to 10. So all of your callbacks are executing the same query for a document that doesn't exist:
FileModel.findOne({servername: "hello10"}
Try this in your node console and see for yourself:
for (var i = 0; i < 10; i++) console.log('i is %d', i );
console.log('afterward i is %d', i) // 10
Sometimes it helps to enable mongoose debug mode to inspect what is getting sent to the db:
mongoose.set('debug', true)