WIL 7주차

jathazp·2022년 2월 21일
0

1. 클론코딩 마무리

저번 주에 진행하던 Velog 클론코딩 마무리를 했다.
팀노션을 통해 일정을 계속해서 조정하고 카테고리별 정렬 기능 등 추가적인 api 구현을 마무리했다.
남은 시간에는 다음 프로젝트를 위한 개인 공부를 진행했다.

2. 실전프로젝트

새로운 조원들과 실전 프로젝트에 진입했다.
이전에 진행했던 프로젝트와 다르게 기간을 길게 잡은 프로젝트이기 때문에 기획에 신경을 많이 썼다.

아직은 본격적인 코딩을 시작하지는 않았고, 와이어 프레임 작성 및 API 설계 중이다. 또, 프로젝트 기획과 협업 방식에 대해 계속해서 이야기하고 수정하는 과정에 있다.
앞으로의 여정이 기대된다.

3. 배운 내용

1. Mongoose query + 정규식+ populate 이용한 필터링 방법

https://mongoosejs.com/docs/api/query.html

let book = await Book.findOne().populate('authors');
book.title; // 'Node.js in Action'
book.authors[0].name; // 'TJ Holowaychuk'
book.authors[1].name; // 'Nathan Rajlich'

let books = await Book.find().populate({
  path: 'authors',
  // `match` and `sort` apply to the Author model,
  // not the Book model. These options do not affect
  // which documents are in `books`, just the order and
  // contents of each book document's `authors`.
  match: { name: new RegExp('.*h.*', 'i') },
  sort: { name: -1 }
});
books[0].title; // 'Node.js in Action'
// Each book's `authors` are sorted by name, descending.
books[0].authors[0].name; // 'TJ Holowaychuk'
books[0].authors[1].name; // 'Marc Harter'

books[1].title; // 'Professional AngularJS'
// Empty array, no authors' name has the letter 'h'
books[1].authors; // []

2. pm2 log 관리

https://pm2.keymetrics.io/docs/usage/log-management/
pm2 logs, pm2 monit

3. Response Method


https://psyhm.tistory.com/7?category=654716

4. Mongoose aggregate

ex1)

$sum: 개수를 count 하거나 value을 더해준다.
$avg: value의 평균값을 구해준다.
$min: 최소값을 구하기
$max: 최대값을 구하기
$push: array값을 result document에 더해준다.
$addtoset: 특정값을 result document에 더해준다. (위 두 expression은 본 collection에 영향을 주지 않는다)
$first ($last): sort을 할 때 첫번째 값이나 마지막값을 구하기 위함

db.products.aggregate([
    {$group:
     {
	 _id:"$manufacturer", 
	 num_products:{$sum:1}
     }
    }
])

ex2)

db.products.aggregate([
    {$group:
     {
	 _id: {
	     "maker":"$manufacturer"
	 },
	 sum_prices:{$sum:"$price"}
     }
    }
])

SQL과 비교하면 다음과 같다.

populate를 활용한 가상필드 생성

blogPostSchema.virtual('author', {
  ref: 'User',
  localField: 'authorId',
  foreignField: '_id',
  justOne: true
});
const User = mongoose.model('User', userSchema);
const BlogPost = mongoose.model('BlogPost', blogPostSchema);
await BlogPost.create({ title: 'Introduction to Mongoose', authorId: 1 });
await User.create({ _id: 1, email: 'test@gmail.com' });

const doc = await BlogPost.findOne().populate('author');
doc.author.email; // 'test@gmail.com'

ref : https://m.blog.naver.com/ijoos/221312444591
https://www.fun-coding.org/mongodb_advanced1.html

Next week

  1. 클론코딩 프로젝트 진행

  2. 테스트 코드 작성

  3. 노마드 코더 zoom 클론 코딩 강의 수강해보기

  4. 알고리즘 스터디

  5. winston 사용 해보기

0개의 댓글