06.node+mongodb 基本CRUD

易俊驰
2023-12-01

1.关于新增

/*
    新增一条数据
 */
var express = require("express");
var mongodb = require("mongodb");
var app = express();
var MongoClient = mongodb.MongoClient;
app.get("/",function(req,res){
    //如果下面地址不存在, 如果往里面插入数据的话,mongo会自动创建这个库
    var url = "mongodb://localhost:27017/company";
    MongoClient.connect(url,function(err,db){
        if(err){
            console.log("数据库连接失败");
            return ;
        }
        console.log("连接成功");
        db.collection("worker").insertOne({
            "name":"李西方",
            "age" :88,
            "worker":"保安大叔",
            "money" :2000,
            "hobby" :["举重","足球"]
        },function(err,data){
            if(err){
                console.log("数据插入失败");
                return ;
            }
            console.log("数据插入成功");
        });
    })
    res.send("部署成功");
});
app.listen(80);
/*
    新增多条
*/
var express = require("express");
var mongodb = require("mongodb");
var app = express();
var MongoClient = mongodb.MongoClient;
app.get("/",function(req,res){
    var url = "mongodb://localhost:27017/company";
    MongoClient.connect(url,function(err,db){
        if(err){
            console.log(" 数据库连接失败");
            return ;
        }
        console.log("数据库连接成功");
        db.collection("worker").deleteMany({});
        db.collection("worker").insertMany([{
            "name":"万幸1",
            "age" :88,
            "worker":"保安大叔",
            "money" :2000,
            "hobby" :["举重","足球"]
        },{
            "name":"李西方",
            "age" :77,
            "worker":"保安大叔",
            "money" :2000,
            "hobby" :["看电视","足球"]
        },{
            "name":"李东方",
            "age" :56,
            "worker":"保安大叔",
            "money" :2000,
            "hobby" :["打牌","足球"]
        },{
            "name":"李南方",
            "age" :73,
            "worker":"保安大叔",
            "money" :2000,
            "hobby" :["篮球","足球"]
        },{
            "name":"李北方",
            "age" :34,
            "worker":"保安大叔",
            "money" :2000,
            "hobby" :["举重","象棋"]
        }],function(err,data){
            if(err){
                console.log("数据插入失败");
                return;
            }
            console.log("数据插入成功");
        });
    });
    res.send();
});
app.listen(80);

2.关于删除

/*
    删除数据
*/
var express = require("express");
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
var app = express();
app.get("/",function(req,res){
    var url = "mongodb://localhost:27017/company";
    MongoClient.connect(url,function(err,db){
        if(err){
            console.log("连接失败");
            return;
        }
        // db.collection("worker").deleteOne({},function (err,data) { 删除所有数据
        db.collection("worker").deleteOne({"name":"万幸1"},function (err,data) {
            if(err){
                console.log("删除数据失败");
                return;
            }
            console.log("删除数据成功");
        });
    });
    res.send();
});
app.listen(80);
/*按ID 删除*/
var express = require("express");
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
var app = express();
app.get("/",function(req,res){
    var url = "mongodb://localhost:27017/company";
    MongoClient.connect(url,function(err,db){
        if(err){
            console.log("连接失败");
            return;
        }
        db.collection("worker").deleteOne({"_id":mongodb.ObjectId("59b23f990268792a4458158f")},function (err,data) {
            if(err){
                console.log("删除数据失败");
                return;
            }
            console.log("删除数据成功");
        });
    });
    res.send();
});
app.listen(80);

3.关于修改

/*
    修改数据
 */
var express = require("express");
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
var app = express();
app.get("/",function(req,res){
    var url = "mongodb://localhost:27017/company";
    MongoClient.connect(url,function(err,db){
        if(err){
            console.log(err);
            return;
        }
        // db.collection("worker").updateOne({"name":"李南方"},{"name":"李西方"},function(err,result){ 完全替换
        // db.collection("worker").updateOne({"name":"李西方"},{$set:{"name":"test1","age":20}},function(err,result){//只修改当前字段
        db.collection("worker").updateMany({"name":"李西方"},{$set:{"name":"test1","age":20}},function(err,result){//批量更新字段
            if(err){
                console.log("修改数据失败");
                return;
            }
            console.log("修改数据成功");
        })
    })
    res.send();
});
app.listen(80);

4.关于查询

/**
 * 查询
 * @type {createApplication}
 */
var express = require("express");
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
var app = express();
app.get("/",function(req,res){
    var url = "mongodb://localhost:27017/company";
    MongoClient.connect(url,function(err,db){
        db.collection("worker").find().toArray(function(err,result){
            res.json(result);
        });
    })
   // res.send();
});
app.listen(80);
var express = require("express");
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
var app = express();
app.get("/",function(req,res){
    var url = "mongodb://localhost:27017/company";
    MongoClient.connect(url,function(err,db){
       var result = db.collection("worker").find();
       console.log(result);
       var arr = [];
       result.each(function (err,v) {
           if(v){
               arr.push(v);
           }else{
               res.json(arr);
           }
       });

    })
    // res.send("end");
});
app.listen(80);
 类似资料: