Nodejs How to use the MONGO database
First, please refer to the following two links to configure and run Express and MongoDB. 1. Use Express to generate a blank website http://cnodejs.org/topic/501f43eef767cc9a51c7b90e 2.mongo database installation and command line connection http://cnodejs.org/ topic/50285b34f767cc9a512a1977
Installing the Mongoose Module
npm install -g mongoose
Mongoose Official website http://mongoosejs.com/
In the Express Routes/index.js file, add the following code
exports.index = function(req, res){ var mongoose = require(‘/usr/local/lib/node_modules/mongoose‘); var db = mongoose.createConnection(‘localhost‘, ‘test‘); var schema = mongoose.Schema({ name: ‘string‘ }); var User = db.model(‘User‘, schema); var kitty = new User({ name: ‘Zildjian‘ }); kitty.save(function (err) { if (err) // ... res.end(‘meow‘); }); User.find({‘name‘:‘Zildjian‘},function(err,docs){ res.render(‘index‘, { title: docs});
Open the Web address http://localhost:3000/you can see the new database record output just now.
Nodejs How to use the MONGO database