Home > Web Front-end > JS Tutorial > body text

How to operate MongoDB using Nodejs

不言
Release: 2018-07-27 11:12:53
Original
1833 people have browsed it

This article introduces you to how to operate MongoDB using Nodejs. It has a good reference value and I hope it can help friends in need.

Use npm to install mongodb first

npm install mongodb
Copy after login

After successful installation, continue to operate in the library and table created by the above operation

Insert

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';    
var insertData = function(db, callback) {  
    //连接到表  
    var collection = db.collection('tb2');
    //插入数据
    var data = [{"name":'wilson001',"age":21},{"name":'wilson002',"age":22}];
    collection.insert(data, function(err, result) { 
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }     
        callback(result);
    });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
    console.log("连接成功!");
    insertData(db, function(result) {
        console.log(result);
        db.close();
    });
});
Copy after login

Query

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';  
var selectData = function(db, callback) {  
  //连接到表  
  var collection = db.collection('tb2');
  //查询数据
  var whereStr = {"name":'wilson001'};
  collection.find(whereStr).toArray(function(err, result) {
    if(err)
    {
      console.log('Error:'+ err);
      return;
    }     
    callback(result);
  });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
  console.log("连接成功!");
  selectData(db, function(result) {
    console.log(result);
    db.close();
  });
});
Copy after login

Modify

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';    
var updateData = function(db, callback) {  
    //连接到表  
    var collection = db.collection('tb2');
    //更新数据
    var whereStr = {"name":'wilson001'};
    var updateStr = {$set: { "age" : 100 }};
    collection.update(whereStr,updateStr, function(err, result) {
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }     
        callback(result);
    });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
    console.log("连接成功!");
    updateData(db, function(result) {
        console.log(result);
        db.close();
    });
});
Copy after login

Delete

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';  
var delData = function(db, callback) {  
  //连接到表  
  var collection = db.collection('tb2');
  //删除数据
  var whereStr = {"name":'wilson001'};
  collection.remove(whereStr, function(err, result) {
    if(err)
    {
      console.log('Error:'+ err);
      return;
    }     
    callback(result);
  });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
  console.log("连接成功!");
  delData(db, function(result) {
    console.log(result);
    db.close();
  });
});
Copy after login

Call stored procedure

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';    
var invokeProcData = function(db, callback) {  
    //存储过程调用
    db.eval('get_tb2_count()', function(err, result) { 
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }             
        callback(result);
    });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
    console.log("连接成功!");
    invokeProcData(db, function(result) {
        console.log(result);
        db.close();
    });
});
Copy after login

Related recommendations:

Addition, deletion and modification of nodes in mongoDB Check the learning

The above is the detailed content of How to operate MongoDB using Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!