Nodejs How to use the MONGO database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.