1. How to start the MongoDB database
Reference Address: http://www.runoob.com/mongodb/mongodb-window-install.html
In the location of the database installation, the Bin folder, enter Mongod--dbpath d:\data\db
D:\data\db is the folder where the data is saved
2. Code
1 varMongoose=require ('Mongoose');2 3 //connecting to a database4Mongoose.connect ('Mongodb://localhost/shu');5 //Create schema6 varSchema=Mongoose. Schema;7 //define the name and type of the field in the table through the schema8 varstudentes=NewSchema ({9 name:string,Ten age:string One }); A //CREATE table using model, student is table name (students in database) -Mongoose.model ('Student', studentes); - //Add Data the //var studentmodel=mongoose.model (' student '); - //var student=new studentmodel (); - //student.name= ' YJ '; - //student.age= ' + '; + //Student.save (function (err) { - //if (err) { + //Console.log (err); A //return; at //}else{ - //console.log (' Mogodb save Successfull '); - //Mongoose.disconnect (); - // } - // }) - //Querying Data in //var studentmodel=mongoose.model (' student '); - //studentmodel.find ({' name ': ' YJ '},function (err, students) { to //Console.log (students) + // }) - the //Change Data * //var studentmodel=mongoose.model (' student '); $ //studentmodel.update ({_id: ' 59f54fdfa35b551b080a0563 '},{age: '},function ' (err, row_updated) {Panax Notoginseng //if (err) { - //Console.log (err); the //return; + //}else{ A //Console.log (row_updated); the // } + // }) - $ //Delete Data $ varStudentmodel=mongoose.model ('Student'); -Studentmodel.findbyid ('59f56db801f75d2a5cafb12d', function (err, student) { - if(err) { the Console.log (err); - return;Wuyi}Else{ the Console.log (student); - //Remove with remove () Wu Student.remove (); - } About})
Using Mongoose to manipulate the MongoDB database