ymwu

导航

node连接mongodb(简略版)

1.先通过配置启动mongodb,然后新建db.js

    已经对相对应的数据库操作增删改查封装完成。

 1 //这个模块里面封装了所有对数据库的常用操作
 2 var MongoClient = require('mongodb').MongoClient;
 3 //封装成为内部函数
 4 function _connectDB(callback) {
 5     var url = "mongodb://@localhost:27017/test";   //从settings文件中,都数据库地址
 6     //连接数据库
 7     MongoClient.connect(url, {useNewUrlParser:true}, function (err, client) {
 8         //client参数就是连接成功之后的mongoclient(个人理解为数据库客户端)
 9         if (err) {
10             console.log("数据库连接失败");
11             return;
12         }
13         console.log("数据库连接成功");
14         //3.0新写法
15         // var db = client.db("test");
16         callback(err, client);
17 
18     });
19 }
20 
21 //插入数据
22 exports.insertOne = function (dbname, collectionName, json, callback) {
23     _connectDB(function (err, db) {
24         const DB = db.db(dbname);
25         DB.collection(collectionName).insertOne(json, function (err, result) {
26             callback(err, result);
27             db.close(); //关闭数据库
28         })
29     })
30 };
31 
32 //查找数据,找到所有数据.
33 exports.find = function (dbname, collectionName, json, callback) {
34     //连接数据库,连接之后查找所有
35     _connectDB(function (err, db) {
36         const DB = db.db(dbname);
37         DB.collection(collectionName).find(json, function (err, result) {
38             callback(err, result);
39             db.close();
40         });
41     });
42 };
43 
44 //删除
45 exports.deleteMany = function (dbname, collectionName, json, callback) {
46     _connectDB(function (err, db) {
47         const DB = db.db(dbname);
48         DB.collection(collectionName).deleteMany(json, function (err, result) {
49             callback(err, result);
50             db.close(); //关闭数据库
51         }
52         );
53     });
54 };
55 
56 //修改
57 exports.updateMany = function (dbname, collectionName, json1, json2, callback) {
58     _connectDB(function (err, db) {
59         const DB = db.db(dbname);
60         DB.collection(collectionName).updateMany(
61             json1,
62             json2,
63             function (err, results) {
64                 callback(err, results);
65                 db.close();
66             });
67     })
68 };

2.在node启动js里面加入相对应的代码

const http = require('http');  //实例化“http”
const fs = require('fs');
const db = require("./model/db.js");
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/index.html', function (req, res) {
    res.sendFile(__dirname + "/" + "index.html");
})
app.listen(process.env.POST || 8081);
// mongodb连接测试
db.insertOne("test", "student", {
    "name": "aaaaaaa",
    "age": 58,
}, function (err, result) {

})
db.deleteMany("test", "student", {
    "name": 'aaaaaaa'
}, function (err, result) {

})
db.updateMany("test", "student", { "name": "lisi" }, {
    $set: {
        "name": "aaaaaaaaaa",
    }
}, function (err, results) {

});
db.find("test", "student", { "name": 'zhangsan' }, function (err, result) {
})

同时使用mongodb可视化工具进行查看,我现在用的是robo-3T

posted on 2018-10-16 17:00  愤怒的小鸟啊啊啊  阅读(201)  评论(0)    收藏  举报