zoukankan      html  css  js  c++  java
  • JS封装 MongoDB

    const MongoClient = require('mongodb').MongoClient;
    const ObjectId = require('mongodb').ObjectId;
    const url = "mongodb://localhost:27017";

    const dbName = 'Javascript语言精粹';
    let obj = {};

    // 连接封装
    function _connect(callback){
        MongoClient.connect(url, function(err, client){
            if(err) throw err;
            callback(client);
            client.close();
        })
    }

    // 创建数据库集合
    obj.create = function(colName, fn){
        _connect(function(client){
            client.db(dbName).createCollection(colName, function(err, res){
                fn(err, res);
            })
        })
    }

    // 插入
    obj.insert = function(colName, dataArr, fn){
        _connect(function(client){
            const col = client.db(dbName).collection(colName);
            col.insert(dataArr, function(err, res){
                fn(err, res);
            })
        });
    }

    // 添加
    obj.save = function(colName, data, fn){
        _connect(function(client){
            const col = client.db(dbName).collection(colName);
            col.save(data, function(err, res){
                fn(err, res);
            });
        })
    }

    // 删除
    obj.remove = function(colName, filter, fn){
        _connect(function(client){
            const col = client.db(dbName).collection(colName);
            col.remove(filter, function(err, res){
                fn(err, res)
            })
        })
    }

    // 更新
    obj.update = function(colName, filterNum, update, fn){
        _connect(function(client){
            const col = client.db(dbName).collection(colName);
            col.update({"_id" : ObjectId(filterNum)}, {$set:update}, function(err, res){
                fn(err, res);
            })
        })
    }

    // 查找
    obj.find = function(colName, filter, fn){
        _connect(function(client){
            const col = client.db(dbName).collection(colName);
            col.find(filter).toArray(function(err, res){
                fn(err, res);
            })
        })
    }

    // 创建索引
    obj.index = function(colName){
        _connect(function(client){
            const col = client.db(dbName).collection(colName);
            col.createIndex({"anystr":"2dsphere"}, function(err, res){
                fn(err, res);
            })
        })
    }


    // obj.find('artical', {}, function(err, res){
    //     if(err) throw err;
    //     console.log(res);
    // })

    module.exports = obj;
  • 相关阅读:
    Sencha Touch id 和 itemId
    解决VS报表.rdl 显示乱码“小方块”问题
    C# 调试程序弹出 没有可用于当前位置的源代码 对话框
    解决DropDownList 有一个无效 SelectedValue,因为它不在项目列表中。这是怎么回事?
    CS0016: 未能写入输出文件“c:windowsMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Filesdata34aae0607daa87dApp_Web_addadvice.aspx.cdcab7d2.ekhlcbjd.dll”--“目录名无效。 ”
    利用微软类库 Visual Studio International Pack 汉字转拼音
    【C#】线程之Parallel
    【C#】线程之Task
    【C#】线程协作式取消
    【C#】属性(Attribute)
  • 原文地址:https://www.cnblogs.com/SharkJiao/p/13921384.html
Copyright © 2011-2022 走看看