Vorbereitung
1. Installieren Sie mongodb über den npm-Befehl
2. Installieren Sie mongodb-Datenbank
CRUD-Vorgang
hier Sie sollten bereits über ein gewisses Verständnis der MongoDB-Datenbank verfügen und einige ihrer Befehle zum Hinzufügen, Löschen, Überprüfen und Ändern kennen.
1. Hinzufügen
var MongoClient = require("mongodb").MongoClient; var DB_URL = "mongodb://localhost:27017/chm"; function insertData(db) { var devices = db.collection('vip'); var data = {"name":"node","age":22,"addr":"nb","addTime":new Date()}; devices.insert(data,function(error, result){ if(error) { console.log('Error:'+ error); }else{ console.log(result.result.n); } db.close(); }); } MongoClient.connect(DB_URL, function(error, db){ console.log('连接成功!'); insertData(db); });
3. Aktualisieren
var mongodb = require('mongodb') var MongoClient = require('mongodb').MongoClient; var DB_CONN_STR = 'mongodb://localhost:27017/chm'; var selectData = function(db, callback) { //连接到表 var collection = db.collection('vip'); //查询数据 var whereStr = {"name":'node'}; collection.find(whereStr,function(error, cursor){ cursor.each(function(error,doc){ if(doc){ //console.log(doc); if (doc.addTime) { console.log("addTime: "+doc.addTime); } } }); }); } MongoClient.connect(DB_CONN_STR, function(err, db) { console.log("连接成功!"); selectData(db, function(result) { console.log(result); db.close(); }); });
Gespeicherte Prozeduren
var MongoClient = require("mongodb").MongoClient; var DB_URL = "mongodb://localhost:27017/chm"; MongoClient.connect(DB_URL, function(error, db){ console.log("连接成功!"); updateData(db); }); function updateData(db) { var devices = db.collection('vip'); var whereData = {"name":"node"} var updateDat = {$set: {"age":26}}; //如果不用$set,替换整条数据 devices.update(whereData, updateDat, function(error, result){ if (error) { console.log('Error:'+ error); }else{ console.log(result); } db.close(); }); }
var MongoClient = require('mongodb').MongoClient; var DB_URL = "mongodb://localhost:27017/chm"; MongoClient.connect(DB_URL, function(error, db){ console.log("连接成功"); deleteData(db); }); function deleteData(db) { var devices = db.collection('vip'); var data = {"name":"node"}; devices.remove(data, function(error, result){ if (error) { console.log('Error:'+ error); }else{ console.log(result.result.n); } db.close(); }) }