• Node js MongoDB简单操作


    //win7环境下node要先安装MongoDB的相关组件(非安装MongoDB数据库),在cmd命令行进入node项目目录后执行以下语句
    //npm install mongodb
    
    //创建连接
    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/testdb";
    
    //链接 testdb 库,不存在则创建
    MongoClient.connect(url, function(error, db) {
        if (error) throw error;
        console.log("testdb 数据库已创建!");
        
        var database = db.db("testdb");
        
        
        //创建集合(表),已有不会报错
        database.createCollection('testtable', function (error, resulat) {
            if (error) throw error;
            console.log("testtable 集合已创建!");
        });
    
    
    
        //向testtable表插入文档(条数据)
        var testdata = { testfield1: "node mongodb", testfield2: "test val" };
        database.collection("testtable").insertOne(testdata, function(error, result) {
            if (error) throw error;
            console.log("文档插入成功");
        });
        
        
        
        //向testtable表插入多条数据
        var testdatas =  [
            { testfield1: 'testval1', testfield2: 'testval2', testfield3: '1'},
            { testfield1: 'testval11', testfield2: 'testval22', testfield3: '2'},
            { testfield1: 'testval111', testfield2: 'testval222', testfield3: '3'}
        ];
        database.collection("testtable").insertMany(testdatas, function(error, result) {
            if (error) throw error;
            console.log("插入的文档数量为: " + result.insertedCount);
        });
        
        //创建并向testtable2表插入多条数据
        database.collection("testtable2").insertMany(testdatas, function(error, result) {
            if (error) throw error;
            console.log("插入的文档数量为: " + result.insertedCount);
        });
        
        
        
        
        
        //更新一条数据
        var whereStr = {"testfield1":'testval1'}; // 查询条件
        var updateStr = {$set: { "testfield2" : "update_testval2" }};
        database.collection("testtable").updateOne(whereStr, updateStr, function(error, result) {
            if (error) throw error;
            console.log("文档更新成功");
        });
        //更新多条数据
        database.collection("testtable").updateMany(whereStr, updateStr, function(error, result) {
            if (error) throw error;
            //如果满足条件的文档对应值已经是要修改的值,此处更新条数为0
            console.log(result.result.nModified + " 条文档被更新");
        });
        
        
        
        //删除一条数据
        var whereStr = {"testfield1":'testval11'}; // 查询条件
        database.collection("testtable").deleteOne(whereStr, function(error, object) {
            if (error) throw error;
            console.log("文档删除成功");
        });
        //删除多条数据
        var whereStr = {"testfield1":'testval111'}; // 查询条件
        database.collection("testtable").deleteMany(whereStr, function(error, object) {
            if (error) throw error;
            console.log(object.result.n + " 条文档被删除");
        });
         
        
        
        //查询testtable表全部数据
        database.collection("testtable"). find().toArray(function(error, result) { // 返回集合中所有数据
            if (error) throw error;
            console.log(result);
        });
        
        
        //也可按条件查询查询testtable表 testfield1 字段等于 testval1 的信息
        var whereStr = {"testfield1":'testval1'}; // 查询条件
        database.collection("testtable"). find(whereStr).toArray(function(error, result) {
            if (error) throw error;
            console.log(result);
        });
        
        
        
        //查询结果排序
        //先按 testfield2 字段升序排列,再按 testfield3 字段降序排列
        var sortStr = { testfield2:1, testfield3: -1 };
        database.collection("testtable").find().sort(sortStr).toArray(function(error, result) {
            if (error) throw error;
            console.log(result);
        });
        
        
        
        //分页查询
        //skip(int)  接受一个数字参数,为返回结果中,跳过指定的条数再显示
        //limit(int) 接受一个数字参数,为返回结果中,限制显示的条数
        //例子将排序后的结果跳过第 1 条后,显示 2 条内容
        var sortStr = { testfield2:1, testfield3: -1 };
        database.collection("testtable").find().skip(1).limit(2).toArray(function(error, result) {
            if (error) throw error;
            console.log(result);
        });
        
        
        
        
        
        
        //多表连接操作
        //mongoDB 不是一个关系型数据库,但可以使用 $lookup 来实现左连接
        //首先是查询的主表(左表)
        database.collection('testtable').aggregate([
            { $lookup:
                {
                    from: 'testtable2',         // 关联的右表
                    localField: 'testfield1',   // 左表要关联的 join 字段
                    foreignField: 'testfield1', // 右表要关联的 join字段
                    as: 'newfield'              // 新生成字段(类型array)
                }
            }
        ], function(error, result) {
            if (error) throw error;
            //console.log(JSON.stringify(result));
            console.log(result);
        });
        
        
    
        
        
        //删除表集合
        database.collection("testtable2").drop(function(error, delOK) { // 执行成功 delOK 返回 true,否则返回 false
            if (error) throw error;
            if (delOK) console.log("集合已删除");
        });
        
        
        
        
        
        db.close();
    });
  • 相关阅读:
    Antelope 和Barracuda区别
    MySQL监控工具-orztop
    MySQL监控工具-orzdba
    CentOS7 下 Hadoop 单节点(伪分布式)部署
    MyBatis-获取 xxxMapper(源码)
    MyBatis-获取 SqlSession(源码)
    Java-获取 JDK 动态代理生成的 Class 文件
    MyBatis-SqlSessionFactory 的创建(源码)
    JAVA-Enum 枚举
    JAVA-Proxy 代理
  • 原文地址:https://www.cnblogs.com/dreamhome/p/8635731.html
Copyright © 2020-2023  润新知