Go to the Bin folder under the folder where MongoDB is installed, execute the command: MONGO, you can use MongoDB.
After installing the mongoose, in App.js, first introduce mongoose this module:
var mongoose = require (' Mongoose ');
Connect to the corresponding database;
Port number default link 27017;
Test is the name of the database, MongoDB does not need to establish a database, when you need to connect the database does not exist, will automatically create a;
Mongoose.connect (' mongodb://localhost/test ');
var con = mongoose.connection;
Con.on (' Error ', function () {
Console.log (' Database connection failed ');
})
Con.on (' Open ', function () {
Database connection succeeded
//1. Defining Schemas
Schema is a database model skeleton that is stored as a file and cannot be manipulated directly against the database. Can be said to be the model skeleton of the data attribute model or collection;
The basic attribute types are string, date, numeric, Boolean, NULL, array, inline document, Mixed (mixed type), ObjectId (object ID), etc.
//Declare field types in two ways, one for the first uppercase field type, and one for the type of lowercase fields that quotation marks contain, such as ' string '
var Schema = new Mongoose. Schema ({
Name:{type:string},
category:{type:string,default: ' Person '}
"
Schema.methods.say = function () {
Console.log ("This is the methods property of the schema object")
//2. Create model, which is a constructor compiled from the schema, or a class, through which the Document object document can be instantiated by model, and the creation and retrieval of documents need to be handled
The collection name is set to the lowercase plural of the model name if it is not the last value, for example, "WORID1" is set to "World1" and "World" is set to "worlds";
Various examples: http://mongoosejs.com/docs/schematypes.html
var Model = Mongoose.model (' World1 ', Schema);
Through the schema, the document in the database is mapped into an object of the program, which has a series of methods such as Save, update, and title, author and so on.
//Document Documentsis the model-created entity, model, andDocument DocumentsCan affect the operation of the database, but the model is more operational;
var dog = new Model ({
name: ' Yuanbao ',
Category: ' Dog '
})
var cat = new Model ({
name: ' Daye ',
Category: ' Cat '
})
Dog.save (function (err,doc) {
//if (ERR) return Console.log (err);
//Model.find ({name: ' Yuanbao '}, (Err,data) =>{
//Console.log (data);
// });
});
Cat.save ();
});
The following links include various method attributes, thanks for sharing.
Reference Links:
Https://www.cnblogs.com/web-fengmin/p/6435681.html
https://cnodejs.org/topic/595d9ad5a4de5625080fe118
Https://www.cnblogs.com/xiaohuochai/p/7215067.html?utm_source=itdadao&utm_medium=referral
In layman mongoose:https://www.villainhr.com/page/2016/05/11/%e6%b7%b1%e5%85%a5%e6%b5%85%e5%87%bamongoose#%e6%b7%b1% E5%85%a5%e6%b5%85%e5%87%bamongoose
Beginner MongoDB and Mongoose