Mongodb queries whether a piece of data exists?
高洛峰
高洛峰 2017-05-02 09:23:23
0
1
590

Suppose I have name=1,name=2 in my database

If you use name=2 to query. When name=2 exists, output the data related to that

Now I use name=3 to query. Add name=3

when name=3 does not exist

How to write using if judgment. .

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
给我你的怀抱
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();
                });
            }
        });
    });
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template