Mongodb queries whether a piece of data exists?

  mongodb, question

If my database has name = 1 and name = 2

If you use name=2 to query. When name=2 exist, data related to that is output

Now I use name=3 to query. Add name=3 when name=3 does not exist

If is used to judge how to write. .

function add(data,col){
 MongoClient.connect(Url, function (err, db) {
 var collection = db.collection(col);
 collection.findOne({'name':data},function(err, doc) {
 if(doc) {
 console.log(doc);
 db.close();
 } else {
 collection.insertOne({'name':data},function () {
 db.close();
 });
 }
 });
 });
 }