is a simple gadget, but there is a big thing, is NoSQL mongodb (file size:130M), you want to download a mongdodb, go to the official website to download
After installation, execute in mongodb command line directory
Mongod--dbpath=c:\mongodbinfo\--port 27017
This command will create a new directory called Mongodinfo in the C drive to store the MONGODB data;
Nodejs to install a dependent library of MongoDB and install it via NPM
NPM Install MongoDB
The MongoDB API can be viewed here to open the API:
These are project-dependent, in fact, Express and MongoDB two, the rest are express comes with:
"Dependencies": { "Cookie-parser": "~1.3.3", "Debug": "~2.0.0", " Body-parser ":" ~1.8.4 ", " Express ":" ~4.9.8 ", " Jade ":" ~1.6.0 ", " MongoDB ":" ~1.3.23 ", " Morgan ":" ~1.3.2 ", " Serve-favicon ":" ~2.1.7 " }
The completed result diagram is this:
Because we have this disk is a database, so at least there is a connection database and operation of the database functions, MongoDB database access to many kinds, mainly because the version is different, the interface has changed, But fortunately the higher version of the database connection is made forward compatible,
The thing that this module does is to connect the database , we can also instantiate Col to Collecionion (Collectioninon is the table in MySQL) to delete and Add Collection:
//Document Address: http://mongodb.github.io/node-mongodb-native/1.4///MongoDB has been updated to 2.0, I take a go;varMongoDB = require (' MongoDB ');//Configuration of database connectionsvarMongourl = "mongodb://localhost:27017/";varOpen =function(name, callback) {/*//1.2 version and 1.4 MongoDB connection mode, 2.0 of the MongoDB connection is forward-compatible; var server = new MongoDB. Server (' localhost ', 27017, {auto_reconnect:true}); var db = new MongoDB. DB (name, server, {safe:true}); Db.open (function (err, db) {if (!err) {console.log (' Connect db '); Callback&&callback (DB); }; }); */ //This kind of connection way is still better understood; varMongoclient = require (' MongoDB '). Mongoclient; //Name for the database; varURL = mongourl+name; Mongoclient.connect (URL,function(err, db) {Console.log (' Connect DB '); Callback&&callback (DB); });};/** @param dbname;* instance @method Create (colname, callback); * Instance @method remove (colname, CA Llback); * Instance @method getdb ();*/varCol =function(name,callback) {//connect to the database;Open (name,function(_db) { This. db =_db; Console.log ("New db is done!") Callback&&callback (_db); }.bind ( This) );}; Col.prototype={constructor:col, create:function(name, callback) { This. Db.createcollection (name, {safe:true},function(Err, collection) {if(Err) {Console.log (err); }Else{callback&&callback (collection); }; }); }, remove:function(name, callback) { This. Db.dropcollection (name, {safe:true},function(err,result) {if(Err) {Console.log (err); return; }; Callback&&callback (result); }); }, Getdb:function() { return This. db; }};module.exports= Col;
And the operation of the data table. I give independent crud of the JS file, the col callback of the database as a parameter passed in will be returned to the database table is a method of pruning and checking , interface parameters (collection name, value, callback function):
Add a piece of data to the Nono table;
Crud.insert ("Nono" [{xx:xx}], callback);
Update Nono all fields with hehe 1 for this table are Lala for Lala, Callback Crud.update ("Nono", {hehe1:1}, {lala: "Lala"},function () {Console.log ("update Done ")}); Crud.remove ("Nono", {hehe:0}, function () {Console.log ("Remove Done")});
Finds the field with the hehe of 1 and returns all values found; Crud.find ("Nono", {hehe1:1}, function (DOC) {}
All APIs have a corresponding API under GitHub's Mongodb-native project, open the API
//Document Address: http://mongodb.github.io/node-mongodb-native/1.4///MongoDB has been updated to 2.0, I take a go;varCrud =function(db) { This. db =DB;}; Crud.prototype={Constructor:crud, NoOp:function(){}, //IncreaseInsert:function(Col, Val, CB) {CB= CB | | This. NoOp; return This. Db.collection (col). Insert (VAL,CB); }, //UpdateUpdate:function(col, search, Val, CB) {CB= CB | | This. NoOp; return This. Db.collection (col). Update (search, {$set: val}, CB); }, //DeleteRemove:function(COL,KEY,CB) {CB= CB | | This. NoOp; //Console.log (This.db.collection (col). Remove); return This. Db.collection (col). Remove (KEY,CB); }, find:function(COL,KEYWORD,CB) {CB= CB | | This. NoOp; This. Db.collection (COL). Find (keyword). ToArray (function(Err, Docs) {CB (DOCS); }); }, findby_id:function(Col,id, CB) { This. Db.collection (COL). Find ({},{_id:id},function(Err, Docs) {Docs.toarray (function(Err,doc) {CB (DOC)})}) }, FindOne:function(COL,KEYWORD,CB) {CB= CB | | This. NoOp; This. Db.collection (COL). FindOne (keyword,function(Err, Docs) {CB (DOCS); }) }};/*The database instance of the problem needs to be put in, var crud = new Crud (db), var result = Crud.insert ("Nono" [{xx:xx}], callback); var result = Crud.upd Ate ("Nono", {hehe1:1}, {lala: "Lala"},function () {console.log ("Update Done")}); var result = Crud.remove ("Nono", {hehe:0}, function () {Console.log ("Remove Done")}); var result = Crud.find ("Nono", {hehe1:1}, function (Doc) {}*/Module.exports=function(db) {return NewCrud (db);};
The main route of the interface has home page, delete, add, update , this four, we have to notice, here is a pit is, the database query ID to be through new ObjectId (ID) to instantiate after the ID, You simply pass a string ID that is not used at all:
varExpress = require (' Express ');varCol = require (".. /mongodb/col.js ");varCrud = require (".. /mongodb/crud.js ");varObjectID = require (' MongoDB '). ObjectID;//routing is to take out the first err;varRouter =Express. Router ();//Initialize CRUDvarcrud;//create a new db and get it;vardb =NewCol ("Todo",function(db) {//database connection Complete ... //create a RESTful object;Crud =NewCrud (DB,function(){} );});/*var data = {title: "T0do", lists: []};*///This can be understood as app.get ("/", Function (req, res, next) {});/*get get all the lists.*/Router.get (‘/‘,function(req, res) {//because the new connection of the DB is asynchronous, so if you create a new database connection, GETDB will not expire immediately, you get undefined;Crud.find ("Todos", {},function(Docs) {console.log (docs); Res.render (' Index ', {title: ' Todos ', Lists:docs}); });});/*The GET user chooses whether to delete the specified ID.*/Router.get ('/del/:id ',function(req, res) {Res.render ("Delete", {id:req.params.id});});//user confirms that TODO with the specified ID is deletedRouter.get ("/del/ok/:id",function(req, res) {varCrud =NewCrud (Db.getdb ()); varID =NewObjectID (req.params.id); Crud.remove ("Todos", {_id:id},function() {Res.redirect (".. /.. /"); });});//get the TODO information interface for editingRouter.get ('/modify/:id ',function(req, res, next) {var_id =NewObjectID (req.params.id); Crud.findone ("Todos", {_id: _id},function(DOC) {doc.id=doc._id; Res.render ("Modify", Doc); });});//Update user information and redirect to the main interfaceRouter.post ('/modify ',function(req, res, next) {varBODY =Req.body; Console.log (body); Crud.update ("Todos", {_id:NewObjectID (Body.id)}, {_id:NewObjectID (body.id), title:body.title,content:body.content},function() {Console.log ("Done"); }); Res.redirect (".. /");});/*GET Add listing.*/Router.get ('/add ',function(req, res) {Res.render ("Add",{});});//The default post value is new and is transferred from the Add interface;Router.post ("/add",function(req, res) {Crud.insert ("Todos", [{title:req.body.title, content:req.body.content}],function() {Console.log ("Success"); }); Res.redirect ("./");}); Module.exports= Router;
Demo Address Click here to download;
Finished
Nodejs+bootstarp+mongodb whole A TODO small example