Mongoose Querying the database steps

Source: Internet
Author: User
Tags mongodb

Build the Dumall database, create goods collections, import data files, or manually insert them yourself. MongoDB Installation and Environment building: http://www.cnblogs.com/ccyinghua/p/7887713.html to create a database and insert data, you can enter the MONGO operations database, insert operations in command line form, That is, after the successful start of MongoDB, as an administrator to open a command line window input MONGO, you can do some database operations, you can also download mongovue3.4.4 (64-bit) client to operate, the client can manually enter the insertion of data, you can import the file to insert data.

First, install the Mongoose

API Document: Http://mongoosejs.com/docs/guide.html

CNPM Install Mongoose--save
Second, create a model models
//Goods.jsvarMongoose = require (' Mongoose '));varSchema =Mongoose. Schema;//Define a schemavarProdutschema =NewSchema ({' ProductId ': String,//or ' productId ': {type:string}' ProductName ': String,' Saleprice ': Number,' Productimage ': String})//output (export)Module.exports = Mongoose.model (' good ', produtschema);//define a good commodity model that can invoke its API methods based on the commodity model. //This model defines the goods collection data for the database Dumall, so the model is named good corresponds to the set, and after the database is connected, the pattern finds the data collection based on the plural form of the name "goods". //module.exports = Mongoose.model (' Good ', Produtschema, ' goods ') or the goods collection of the database can be noted later
third, connect the database
//Db.jsvarMongoose = require (' Mongoose '));varGoods = require ('./goods.js '));//link MongoDB Database, the name of the database is called DumallMongoose.connect (' Mongodb://127.0.0.1:27017/dumall ');//If you have an account password: ' Mongodb://root:[email protected]:27017/dumall '//Connection Successful OperationMongoose.connection.on ("Connected",function() {Console.log ("MongoDB connected success.")})//Connection failed ActionMongoose.connection.on ("Error",function() {Console.log ("MongoDB connected fail.")})//Connection Disconnect OperationMongoose.connection.on ("Disconnected",function() {Console.log ("MongoDB connected disconnected.")})//After the connection is successful, the goods collection of the database is queried using the model's good commodity models. Goods.find ({},function(err, doc) {if(Err) {Console.log (err.message)}Else{Console.log (DOC)}})

Contents of Doc:

Mongoose Querying the database steps

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.