I. Cấu hình MongoDB
1. Dùng trên Localhost: MongoDB Compass
- Download: https://www.mongodb.com/try
- Vào Onpremises -> MingoDB Community Server -> Download -> Install bình thường
- Để link truy cập Localhost tại file .env
MONGO_LOCALHOST=mongodb://localhost:27017/medihome
2. Tạo Project trên MongoDB
1.1. Tạo Cluster
- Đăng nhập -> Tạo Project (2 bước: Tên Project và add Member)
- Build Cluster
1.2. Connect Cluster
- Cài Network Access: IP được phép truy cập trước (nên chọn 0.0.0.0/0 -> có thể truy cập mọi nơi để tránh phát sinh lỗi)
- Cài tài khoản có quyền truy cập
1.3. Cấu hình
- Để link truy cập MongoDBtại file .env
MONGODB_URI=mongodb+srv://root:[email protected]/test?retryWrites=true
II. Kết nối MongoDB
1. Cấu hình file ./.env
1 2 3 |
MONGO_LOCALHOST=mongodb://localhost:27017/medihome MONGODB_DB_NAME=medihome |
2. Dùng mongodb
1.1. Cài đặt thư viện:
https://www.npmjs.com/package/mongodb
npm i mongodb
1.2. Tạo file: src/config/database/mongoDB.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
const MongoClient = require('mongodb').MongoClient; let _mongodb; let options = { useNewUrlParser: true, useUnifiedTopology: true }; let connect = async (uri, dbname) => { try { let client = new MongoClient(uri, options); let _connection = await client.connect(); _mongodb = _connection.db(dbname); console.log('\x1b[33m' + 'MongoDB connect successfully !!!' + '\x1b[0m'); } catch (error) { console.log('\x1b[33m\x1b[41m' + error + '\x1b[0m'); } }; let getDB = () => { return _mongodb; }; module.exports = { getDB, connect }; |
1.3. Tại file: src/server.js
1 2 3 |
const uri = process.env.MONGODB_URI || process.env.MONGO_LOCALHOST; const mongoDB = require('./config/database/mongoDB'); mongoDB.connect(uri, process.env.MONGODB_DB_NAME); |
1.4. Sử dụng
1 2 3 4 5 6 7 8 |
const mongoDB = require('../../../config/database/mongoDB'); const ObjectId = require('mongodb').ObjectId; let collection = mongoDB.getDB().collection('phieunhapthuocs'); collection.insertOne(req.body, (error, results) => { if (error) throw error; console.log('Insert One, Have Done !!!'); }); |
3. Dùng mongoose
2.1. Cài đặt thư viện:
-- Github: https://github.com/Automattic/mongoose
npm install mongoose
3.2. Tạo file: src/config/database/mongo.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const mongoose = require('mongoose'); connect = (uri) => { let options = { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false, } mongoose.connect(uri, options) .then(() => console.log('Connect successfully !!!')) .catch((error) => console.log('Connect failure !!!')) } module.exports = { connect }; |
3.3. Tại file: src/server.js
1 2 3 |
const uri = process.env.MONGODB_URI || process.env.MONGO_LOCALHOST; const mongo = require('./config/database/mongo'); mongo.connect(uri); |
3.4. Sử dụng
1 2 3 4 5 6 7 8 |
const mongoDB = require('../../../config/database/mongoDB'); mongoDB.getDB().collection('users').find({}, function (error, result) { if (error) throw error; result.toArray(function (err, list) { console.log(list[0]); }); }); |