zoukankan      html  css  js  c++  java
  • 【node】node连接mongodb操作数据库

    1、下载第三方模块mongodb

    cnpm install mongodb --save
    2、检测是否连接成功
    复制代码
    1、引入第三方模块mongodb并创建一个客户端
    
    const MongoClient = require("mongodb").MongoClient;
    
    2、连接数据库
    //连接地址
    const url = "mongodb://127.0.0.1:27017";
    
    //连接数据库的名称
    const db_name = "test";
    
    //检测是否连接成功
    MongoClient.connect(url,(err,client)=>{
        console.log(err,client);
    })
    复制代码

    3、连接数据库并选用数据库中的哪张表

    复制代码
    const MongoClient = require("mongodb").MongoClient;
     
    const url = "mongodb://127.0.0.1:27017";
     
    const db_name = "test";
     
    MongoClient.connect(url,(err,client)=>{
     
        //连接db_name这个数据库并使用student这张表
        const collection = client.db(db_name).collection('student');
    })
    复制代码

    4、增

    复制代码
    //引入第三方模块mongodb并创建一个客户端
    const MongoClient = require("mongodb").MongoClient;
    
    //定义连接的地址
    const url = "mongodb://127.0.0.1";
    
    //定义连接的数据库
    const db_name = "test";
    
    //客户端连接数据库
    MongoClient.connect(url,(err,client)=>{
        //连接db_name这个数据库并使用student这个表
        const collection = client.db(db_name).collection("student");
        
        //存入数据并退出连接
        collection.save(
            {
                name:"德玛西亚",
                age:25,
                sex:"男"
            },
            (err,result)=>{
                client.close();
            }
        )
    })
    复制代码

    5、删

    复制代码
    //引入第三方模块mongodb并创建一个客户端
    const MongoClient = require("mongodb").Mongoclient;
    
    //定义连接的地址
    const url = "mongodb://127.0.0.1:27017";
    
    //定义连接的数据库
    const db_name = "test";
    
    //客户端连接数据库
    MongoClient.connect(url,(err,client)=>{
        //连接db_name这个数据库并使用student这个表
        const collection = client.db(db_name).collection("student");
        
        //删除指定数据并退出连接
        collection.remove(
            {
                name:"德玛西亚"
            },
            (err,result)=>{
                client.close();
            }
        )
    })
    复制代码

    6、改

    复制代码
    //引入第三方模块mongodb并创建一个客户端
    const MongoClient = require("mongodb").MongoClient;
    
    //定义连接的地址
    const url = "mongodb://127.0.0.1:27017";
    
    //定义连接的数据库
    const db_name = "test";
    
    //客户端连接数据库
    MongoClient.connect(url,(err,client)=>{
    
         //连接db_name这个数据库并使用student这个表
        const collection = client.db(db_name).collection("student");
        
        //更新指定数据并退出连接
        collection.update(
            {
                name:"德玛西亚"
            },
            {
                $set:{name:"提莫队长"}
            }
            (err,result)=>{
                client.close();
            }
        )
    })
    复制代码

    7、查

    复制代码
    //引入第三方模块mongodb并创建一个客户端
    const MongoClient = require("mongodb").MongoClient;
    
    //定义连接的地址
    const url = "mongodb://127.0.0.1:27017";
    
    //定义连接的数据库
    const db_name = "test";
    
    //客户端连接数据库
    MongoClient.connect(url,(err,client)=>{
    
         //连接db_name这个数据库并使用student这个表
        const collection = client.db(db_name).collection("student");
    
        //查找到所有数据并转化成一个数组
        collection.find().toArray((err,result)=>{
            console.log(result);
            client.close();
        })
    })
    复制代码
  • 相关阅读:
    35 个手机和Web应用开发图标集
    20个创意404错误页面设计的启示
    31个不同种类的网站免费滑块巨大集合(PSD文件)
    18个伟大的搜索引擎设计者能够找到高品质的免费图标
    50 个独家免费图标集下载
    C语言对结构体何时用> , 何时用.
    GNU make manual 翻译(一)
    PostgreSQL 的 语法分析调用关系
    GNU make manual 翻译(二)
    PostgreSQL 的 target_list分析(七)
  • 原文地址:https://www.cnblogs.com/liuhaov/p/13503245.html
Copyright © 2011-2022 走看看