You will have to provide a callback to the write functions as you have writeConcern 1(its the default) when you created the connection to database. Your code should like this:
db.createCollection("category",function(errDb,collection){
collection.findOne({name:"test"},function(err,value){
if(value == null)
{
collection.insert({name:"test"}, function(err, docs){
if (err) //do anything you want to do when there is an error
// Here you write code for what you would do when the documents were sucessfully inserted into the collection
});
}
})
})
But if you don't want the write concern to be activated and just want to insert using the function that you had done then when you create the connection, you will have to connect like this.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/integration_test_?", {
db: {
w : o
}
}, function(err, db) {
test.equal(null, err);
test.ok(db != null);
// Do anything like you were doing here
}
Here I set the writeConcern to 0. This prevents mongo.db to confirm you about the success of insert or update. So now you can just use collection.insert({name:"test"});
Just A word of concern be careful in using with w:0 as you wouldn't want to be using it for datas which you want to sure about the insert or update as you might lose some data due to unsuccessful writes.