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