If you don't know this before reading this article, you won't simply use mongodb. Stamp here
1. Install mongodb dependency, here we install v2.2.33, if we've encountered it beforeDb.collectionIs not function is really just a mongodb library version issue (desktop creation of fml files)
My project file name is fml C:\Users\admin\Desktop\fml> npm install mongodb@2.2.33
2. Insert data
New node-in fml Mongodb.jsAnd copy and paste the code below
var MongoClient = require('mongodb').MongoClient; // Connection Database String Form var DB_CONNECT_STR = 'mongodb://localhost:27017/fengml'; var insertData = function(db, callback) { //Connect to table fmlsite var collect = db.collection('fmenglsite'); //insert data var data = [{"author":"Fengmuli","url":"http://www.jianshu.com/u/96432503edfd","description":"good good study"}]; collect.insert(data, function(err, result) { if(err){ console.log('Error:'+ err); return; } callback(result); }); } MongoClient.connect(DB_CONNECT_STR, function(err, db) { console.log("connect server success!"); insertData(db, function(result) { console.log(result); db.close(); }); });
If you are also a user of vscode, the shortcut key Ctrl+`Execute node-Mongodb.js
C:\Users\admin\Desktop\fml> node node-mongodb.js connect server success! { result: { ok: 1, n: 1 }, ops: [ { author: 'Fengmuli', url: 'http://www.jianshu.com/u/96432503edfd', description:'good good study', _id: 5a28ffd2241ba235a4d51535 } ], insertedCount: 1, insertedIds: [ 5a28ffd2241ba235a4d51535 ] }
You can also view it through the mongodb client using syntax such as show dbs ( Stamp here if you forget)
3. Query data
var MongoClient = require('mongodb').MongoClient; var DB_CONNECT_STR = 'mongodb://localhost:27017/fengml'; var searchData = function(db, callback) { //Connect to a table var collection = db.collection('fengmlsite'); //Query Data var dataStr = {"description":'good good study'}; collection.find(dataStr).toArray(function(err, result) { if(err){ console.log('Error:'+ err); return; } callback(result); }); } MongoClient.connect(DB_CONNECT_STR, function(err, db) { console.log("connect server success"); searchData(db, function(result) { console.log(result); db.close(); }); });
Printing results (if returned to [], you may need to wait a while, perhaps)
C:\Users\admin\Desktop\fml> node node-search.js connect server success [ { _id: 5a2907838e6f0939908741a9, author: 'Fengmuli', url: 'http://www.jianshu.com/u/96432503edfd', description: 'good good study' } ]
4. Update Data
var MongoClient = require('mongodb').MongoClient; var DB_CONNECT_STR = 'mongodb://localhost:27017/fengml'; var updateData = function(db, callback) { //Connect to a table var collection = db.collection('fengmlsite'); //Update Data var dataStr = {"author":'Fengmuli'}; var updateStr = {$set: { "description" : "day day up" }}; collection.update(dataStr,updateStr, function(err, result) { if(err){ console.log('Error:'+ err); console.log(0) return; } callback(result); }); } MongoClient.connect(DB_CONNECT_STR, function(err, db) { console.log("connect server success"); updateData(db, function(result) { console.log(result); db.close(); }); });
Terminal Input
C:\Users\admin\Desktop\fml> node node-update.js We rerun node-Search.jsWe'll find that the result is empty, and we're putting node-Search.jsChange the query condition datastr to {"author":'Fengmuli'}.You will see the following results: C:\Users\admin\Desktop\fml> node node-search.js connect server success [ { _id: 5a2907838e6f0939908741a9, author:'Wind Muli', url: 'http://www.jianshu.com/u/96432503edfd', description: 'day day up' } ] You will see that the description field has been updated
5. Delete data
var MongoClient = require('mongodb').MongoClient; var DB_CONNECT_STR = 'mongodb://localhost:27017/fengml'; var delData = function(db, callback) { //Connect to a table var collection = db.collection('fengmlsite'); //Delete data var dataStr = {"author":'Fengmuli'}; collection.remove(dataStr, function(err, result) { if(err){ console.log('Error:'+ err); return; } callback(result); }); } MongoClient.connect(DB_CONNECT_STR, function(err, db) { console.log("connect server success!"); delData(db, function(result) { console.log(result); db.close(); }); });
Terminal Input
C:\Users\admin\Desktop\fml> node node-delete.js We rerun node-Search.jsThe result is empty, indicating that the deletion was successful
Note: Remember to run the mongodb service before you start (run manually)Mongod.exeandMongo.exe Or by command)
PS: In this way, the front-end can operate the background happily. For us at the front-end, it is self-evident that we are happy.