반응형
NoSql중 Node.js 아래의 MongoDb 기본 Connection 부분을 간략히 설명.
1. npm install mongoose
2. connection.js 접속정보 입력
module.exports = async () => {
const mongoose = require('mongoose');
const mongoConfig = require('../../config/mongo.config');
await mongoose.connect(mongoConfig.mongoURI,{
useNewUrlParser :true,
useUnifiedTopology :true,
useFindAndModify :false
}).then(()=>{
console.log('mongoDB Connected...');
}).catch(err =>{
console.log(err);
})
return mongoose;
}
- mongo.config(샘플)
module.exports = {
mongoURI : "mongodb://127.0.0.1:27017/testDb?retryWrites=true&w=majority"
}
3. sample service.js
/**
* Sample Service
*
* @author mkhwang
* @description Sample Service *
*/
const mongoRoomChat = require("../../mongoModels/RoomChat");
/**
* sample service list select
* @param {test_seq} 채팅 방 seq
*/
exports.getPreChatList = async (obj) => {
try{
if(isEmpty(obj))
{
return api.parameterNull;
}
else
{
let conversationList = await mongoRoomChat.find({"test_seq":obj.test_seq}).sort({date: -1}).skip(obj.page_size*(obj.page_num-1)).limit(obj.page_size);
return objectAssign({"page_size":obj.page_size, "page_num":obj.page_num, "list":conversationList});
}
} catch (e) {
return objectAssign({"error":e.toString()}, api.fail) ;
}
sort - 정렬할 field : 1 or -1 (1일 경우 오름차순, -1일 경우에 내림차순.)
limit - 출력할 갯수를 제한
skip - 출력할 데이터의 시작을 설정. 즉 인자로 들어오는 value 만큼 건너뛰고 value+1부터 출력
반응형