Egg-js connection mongDb

Open the Mongoose plugin method: if the project plugin.js file is created using eggJS, it looks like this

module.exports = {
	mongoose: { 
    		enable: true, 
        	package: 'egg-mongoose'
    	}
 };
Copy the code

Config.default. js config.default.js config.default.js config.default.js config.default.js

modules.exports= appInfo =>{ const config = exports = {}; The config. Mongose = {client: {url: 'mongo: / / 127.0.0.1:7001 /? GssapiServiceName = name of database summary Db '}}}Copy the code

Note: mongodb:// 127.0.1:7001 indicates the connected database mongodb://127.0.0.1 indicates the address. 7001 indicates the port number

To configure multiple databases at the same time, see

With the Egg-Mongoose plug-in, the most important thing is the Schema method

-sheldon: Yes, yes, yes, yes, yes, yes, yes, yes

Parameter listing rules: The input parameter fields of an interface do not have to have the same number of fields as those declared in the Schema, but the input parameter fields must exist in the Schema.

{userName:' zhang SAN '} Schema field {userName: {type:'String'}, age:{type:'Number'}} Example: Sex :' male '} Schema field {userName:{type:'String'}, age:{type:'Number'}}Copy the code

In the figure, the first parameter in return represents the name of the table in mongDb (case insensitive), and the second parameter represents the variable name after the new Schema is instantiated (UserSchma in the figure). If the database does not have a corresponding table name, don’t worry, because MONG will automatically create a table name with the same name.

Look for plug-ins directly on the Egg website