node. JS development----Create and connect database MongoDB

Source: Internet
Author: User
Tags mongodb download node server



1.MongoDB Download Install E:\MongoDB






2. Where to create the database file E:\MongoDB\data\db















3. Start MongoDB Service















Library operations
New database: The first step: Use the new database name; second step: Do the library related operations; If you do not take the second step, the database will not be created
View database: Show DBS;
New table: Db.createcollection (' new table name to be created ');
View current database table below: Show collections;
Deletes the current database specified table: DB. Table name. Drop ();
Delete Current database: Db.dropdatabase ();



---------------------------------------------------------------I'm a split line------------------------------------------------------






Write a simple demo below.



1.express Creating E:\test Projects



Don't point this






2. Create a database



E:\MongoDB\bin Start Mongo.exe



Input



Use text \ Create a database called text
Db.createcollection ("users") \ Create a collection
Db.users.insert ({"Name": "admin", "Password": "111"}) \ adds a document to the Users collection.
Db.users.find () \ Query the document you added












3. Project Connection Database



Create a folder under the project root directory database, and then create a db.js E:\test \ Database\db.js





[HTML] View plain copy

var mongoose = require (' Mongoose ');

var db = mongoose.connect (' Mongodb://localhost/text ');//; Connect to database

var Schema = Mongoose.   Schema; Create a model

var userschema = new Schema ({

Name:string,

Password:string

}); A new model has been defined, but this pattern has not been associated with the Users collection

Exports.user = Db.model (' users ', userschema);//associated with the Users collection












4.views Folder CREATE View file






5. Control of routing
Index.js in the routes file






6. Start the node server



View in Http://localhost:3000/
























Demo File: Https://github.com/MMMsCheng/nodedemoLogin






node. JS development----Create and connect database MongoDB


Related Article

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.