0%

node操作MongoDB

居安思危,思则有备,有备无患。 —— 《左传·襄公十一年》

一、准备阶段

  1. 安装node
  2. 安装MongoDB

二、开始(Mac)

  1. 进入主目录cd ~

  2. 创建node项目目录mkdir node

  3. 安装依赖npm install mongodb

  4. 开撸

    • 创建文档
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    var MongoClient = require('mongodb').MongoClient;
    var url = 'mongodb://localhost:27017/';

    MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true}, function (err, con) {
    if (err) {
    throw err;
    }
    var db = con.db("mydb");
    db.createCollection('student', function (err, res) {
    if (err) {
    throw err;
    }
    console.log("Create dbs and collections success !");
    con.close();
    });
    });
    • 插入
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/";
    MongoClient.connect(url, {useUnifiedTopology: true, useNewUrlParser: true}, function (err, con) {
    if (err) {
    console.log("Connect error");
    }
    var db = con.db('mydb');
    var obj = {"name" : "张三", "age" : 12};
    db.collection("student").insertOne(obj, function (err, res) {
    if (err) {
    console.log("InsertOne fail");
    }
    // console.log(res);
    console.log("InsertOne success");
    con.close();
    });
    var obj = [
    {"name" : "张三", "age" : 12},
    {"name" : "李四", "age" : 13},
    {"name" : "王五", "age" : 14},
    ];
    db.collection("student").insertMany(obj, function (err, res) {
    if (err) {
    console.log("InsertMany fail");
    }
    // console.log(res);
    console.log("InsertMany success");
    con.close();
    });
    })
    • 查询
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/";

    MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, con) {
    if (err) {
    console.log("Connect error");
    }
    var db = con.db("mydb");
    var filter = {"name":"张三"}; // 查询条件
    var fields = {age: 0}; // 过滤字段
    // db.collection("student").find(filter).project(fields).toArray(function (err, result) {
    db.collection("student").find(filter, {fields: {name: 1}}).toArray(function (err, result) {
    // db.collection("student").find(filter, {projection: {age:0}}).toArray(function (err, result) {
    // db.collection("student").find(filter, fields).toArray(function (err, result) { // 不起作用
    if (err) {
    throw err;
    }
    console.log(result);
    con.close();
    });
    });
    • 更新
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/";

    MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, con) {
    if (err) {
    console.log("Connect error");
    }
    var db = con.db("mydb");
    var filter = {"name":"张三"};
    var replace = {$set: {"name":"张五"}};
    db.collection("student").updateOne(filter, replace, function(err, res) {
    if (err) {
    throw err;
    }
    console.log("Update one success");
    con.close();
    });

    var filter = {"name":"张三"};
    var replace = {$set: {"name":"张五"}};
    db.collection("student").updateMany(filter, replace, function(err, res) {
    if (err) {
    throw err;
    }
    console.log("Update many success");
    con.close();
    });
    });


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

    MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, con) {
    if (err) {
    throw err;
    }
    var db = con.db("mydb");
    var filter = {name: "李四"}; // 查询条件
    var replace = {$set: {name: "张三"}}; // 替换
    db.collection("student").replaceOne(filter, replace, function(err, res) {
    if (err) {
    throw err;
    }
    console.log(res.result.n + "条被替换");
    con.close();
    });
    });
    • 删除
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/";

    MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, con) {
    if (err) {
    throw err;
    }
    var db = con.db("mydb");
    var filter = {name: "张五" }; // 查询条件
    db.collection("student").deleteMany(filter, function (err, res) {
    if (err) {
    throw err;
    }
    console.log(res.result.n + " 条文档被删除");
    con.close();
    });
    });

    三、参考

    1. node packages