(二)MongoDB在nodejs中的简单封装

// 封装数据库

var MongoClient = require('mongodb').MongoClient;

var Config = require('./config');

var ObjectId = require('mongodb').ObjectId

class Db {

    // 单例模式
    static getInstance() {
        if (!Db.instance) {
            Db.instance = new Db();
        }
        return Db.instance;
    }

    constructor () {
        // console.log('数据库构造函数');
        this._dbClient = '';
        this.connect(); // 被引用时,直接连接数据库
    }

    getObjectId (_id) {
        return new ObjectId(_id);
    }

    connect() {
        return new Promise((resolve, reject) => {

            if (!this._dbClient) {
                MongoClient.connect(Config.dbUrl, { useNewUrlParser: true }, (err, client) => {

                    if (err) {
                        reject(err);
                    } else {
                        this._dbClient = client.db(Config.dbName);
                        resolve(this._dbClient);
                    }

                });
            } else {
                resolve(this._dbClient);
            }


        })
    }

    find (collectionName, json) {

        return new Promise((resolve, reject) => {
            this.connect().then(db => {

                var result = db.collection(collectionName).find(json);

                result.toArray((err, docs) => {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(docs);
                    }
                })

            });
        });

    }

    insert (collectionName, json) {

        return new Promise((resolve, reject) => {

            this.connect().then(db => {

                db.collection(collectionName).insertOne(json, (err, result) => {

                    if (err) {
                        console.log('新增失败');
                        reject(err);
                    } else {
                        resolve(result);
                    }

                });

            });

        });

    }

    update (collectionName, json1, json2) {
        return new Promise((resolve, reject) => {

            this.connect().then(db => {

                db.collection(collectionName).updateOne(json1, {
                    $set: json2
                }, (err, result) => {

                    if (err) {
                        console.log('修改失败');
                        reject(err);
                    } else {
                        resolve(result);
                    }

                });

            });

        });
    }

    remove (collectionName, json) {

        return new Promise((resolve, reject) => {

            this.connect().then(db => {

                db.collection(collectionName).deleteOne(json, (err, result) => {

                    if (err) {
                        console.log('新增失败');
                        reject(err);
                    } else {
                        resolve(result);
                    }

                });

            });

        });

    }

}


/*var db1 = Db.getInstance();
console.time('start');
db1.find('user', {}).then(data => {
    console.timeEnd('start');
});
console.time('start1');
db1.find('user', {}).then(data => {
    console.timeEnd('start1');
});

var db2 = Db.getInstance();

console.time('start2');
db2.find('user', {}).then(data => {
    console.timeEnd('start2');
});
console.time('start3');
db2.find('user', {}).then(data => {
    console.timeEnd('start3');
});*/


module.exports = Db.getInstance();

 

你可能感兴趣的:(MongoDB)