아하
검색 이미지
생활꿀팁 이미지
생활꿀팁생활
생활꿀팁 이미지
생활꿀팁생활
완강한레아173
완강한레아17320.06.27

몽고 DB 디버깅은 어떻게 하나요?

몽고DB+nodejs 로 게시판 만들기를 하고 있습니다.

DB에서 category에 'test11_board' 값을 가지고 있는 글만 쫙 뽑아내고 싶습니다. & 글 갯수를 뽑 아내고 싶습니다.

글만 뽑아내는건 했는데 갯수를 뽑는게 잘 안되네요..

전에 노드에서는

console.log 나 dir을 통해서 값이 어디서 잘못 된건지 파악 하곤 했었는데요.

몽고DB가 더해지니 디버깅을 어떻게 해야할지 모르겠네요.

const Database = require('../database/database'); const DateFormat = require('date-format'); async function countWithWriterSearch (searchType, searchTerm) { const UserModel = Database.getMongooseModel('UserModel'); const writerIds = await UserModel.find({ name: { $regex: '.*' + searchTerm + '.*' } }, '_id'); if (!writerIds || writerIds.length === 0) { return 0; } const PostModel = Database.getMongooseModel('PostModel'); return await PostModel.countDocuments({ category: 'test11_board' },{ name: { $in: writerIds } }); ▶▶▶▶▶이부분쯤에서 검출된 글 갯수를 디버깅하고싶습니다. } async function countWithSearch (searchType, searchTerm) { const PostModel = Database.getMongooseModel('PostModel'); const condition = {}; if (searchType) { condition[searchType] = { category: 'test11_board' }; } return await PostModel.countDocuments(condition); } async function findAllWithPagingAndWriterSearch (offset, perPage, searchType, searchTerm) { const UserModel = Database.getMongooseModel('UserModel'); const writerIds = await UserModel.find({ name: { $regex: '.*' + searchTerm + '.*' } }, '_id'); if (!writerIds || writerIds.length === 0) { return []; } const PostModel = Database.getMongooseModel('PostModel'); return await PostModel.find({ category: 'test11_board' }, '_id title viewcount created_at') .populate('writer') .skip(offset) .limit(perPage) .sort({ created_at: -1 }); } async function findAllWithPagingAndSearch (offset, perPage, searchType, searchTerm) { const PostModel = Database.getMongooseModel('PostModel'); const condition = {}; if (searchType === 'title' || searchType === 'contents') { condition[searchType] = { category: 'test11_board' }; } return await PostModel.find({ category: 'test11_board' }, '_id title viewcount created_at') .populate('writer') .skip(offset) .limit(perPage) .sort({ created_at: -1 }); } return { category: post.category, id: post._id, title: post.title, contents: post.contents, comments: post.comments, password: post.password, isUseComments: post.isUseComments, isUnKnownWriter: !post.writer, username: post.writer ? post.writer.name : '[비회원]', userId: post.writer ? post.writer._id : null, likeCount: post.likes.length || 0, hateCount: post.hates.length || 0, createdAt: DateFormat.asString('yyyy-MM-dd hh', post.created_at) }; }; ▶DB 스키마 category: {type: String, trim: true, 'default':''}, title: {type: String, trim: true, 'default':''}, contents: {type: String, trim: true, 'default':''}, writer: {type: mongoose.Schema.ObjectId, ref: 'users6'}, viewcount: {type: Number, default:0}, password: {type: String, maxlength: 4}, isUseComments: { type: Boolean, default: true }, likes: [{type: mongoose.Schema.ObjectId, ref: 'users6'}], hates: [{type: mongoose.Schema.ObjectId, ref: 'users6'}], comments: [{ contents: {type: String, trim:true, 'default': ''}, writer: {type: mongoose.Schema.ObjectId, ref: 'users6'}, created_at: {type: Date, 'default': Date.now} }], tags: {type: [], 'default': ''}, created_at: {type: Date, index: {unique: false}, 'default': Date.now}, updated_at: {type: Date, index: {unique: false}, 'default': Date.now} });

55글자 더 채워주세요.
답변의 개수
1개의 답변이 있어요!
  • 안녕하세요.

    똑같이 return값을 찍어보시면 됩니다.

    작성해 주신 code는 promise와 await를 사용한 것으로, 아래와 같이 살짝만 변경해서 찍어보시면 됩니다.

    return await PostModel.countDocuments({ category: 'test11_board' },{ name: { $in: writerIds } });

    const dbSize = await PostModel.countDocuments({ category: 'test11_board' },{ name: { $in: writerIds } }); console.log(dbSize); return dbSize;

    와 같이 바꾸셔서 console.log로 찍어보시면 됩니다.