The mongodb database is a non-relational database (nosql) stored in the form of files. Mongodb does not have the concept of rows and tables in relational databases, but has a similar concept of document collections. A document is the most basic unit of MongoDB. A collection is the sum of multiple documents, a collection has multiple documents, and a database has multiple collections.
Install the database before connecting to the database: The following is the installation database for the linux (64-bit) platform:
First go to the official website to download the mongodb version suitable for your computer: http://www.mongodb.org/downloads
After downloading the installation package, unzip it, and extract the installation package to the specified path. Then change the name to mongodb.
I decompress it to the /usr/local/ directory.
Set environment variables:
The executable file of mongodb is in the bin directory, you can set it to the path path:
export PATH=</usr/local/mongodb>/bin:$PATH
You can enter the command in the terminal: ./mongo to see if you have mongodb installed.
Step 2: Create a new folder blog (your project name) in mongodb and create a folder mongodb under /var/log/ to store the log, then enter the bin directory and enter the command at the terminal:
./mongod --dbpath=/usr/local/mongodb/blog/ --port=27017 --fork --logpath=/var/log/mongodb/mongodb.log
Command: The blog folder is the storage directory for our work and starts the database.
Connect to the database:
Open the package.json file and add in the last line of dependencies:
"mongodb": "1.4.15"
Then run in the project directory: npm install to update the dependent modules.
After the mongodb module is installed, create a settings.js file in the project directory and add:
module.exports = { cookieSecret: 'myblog', db: 'blog', host: 'localhost', port: 27017 };
Then create a new models folder, and create a new db.js file in the folder, add:
var settings = require('../settings'), Db = require('mongodb').Db, Connection = require('mongodb').Connection, Server = require('mongodb').Server; module.exports = new Db(settings.db, new Server(settings.host, settings.port), {safe: true});
Then open app.js, in
var routes = require('./routes/index');下添加:
var settings = require('./settings');
Session support:
To store session information in the database for persistent maintenance, we need the help of two middleware, express-session and connect-mongo. In the package.json file add:
"express-session": "1.9.1", "connect-mongo": "0.4.1"
After updating the module npm install, open the app.js file and add:
var session = require('express-session'); var MongoStore = require('connect-mongo')(session); app.use(session({ secret: settings.cookieSecret, key: settings.db,//cookie name cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days store: new MongoStore({ db: settings.db, host: settings.host, port: settings.port }) }));
Note: Here is a reminder that the location of these two pieces of code is a problem. The above two variables are placed in the position of the variables in the file. The following paragraph should be placed in:
app.use(express.static(path.join(__dirname, 'public')));
above this code.