Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dev-Log] 진가람 2021-12-17 #498

Open
heartane opened this issue Dec 17, 2021 · 0 comments
Open

[Dev-Log] 진가람 2021-12-17 #498

heartane opened this issue Dec 17, 2021 · 0 comments
Assignees
Labels
dev-log dev 로그 작성시 사용하는 라벨

Comments

@heartane
Copy link
Collaborator

heartane commented Dec 17, 2021

오늘은 어떻게 프로젝트에 기여했나요?

오늘의 프로젝트에서 힘든 점은 무엇인가요?

  • 검색어로 Join된 Docs 찾기
    쿼리로 온 키워드로 문서를 찾는 검색기능을
    populate된 문서의 필드(foreignField)도 포함하여 검색을 구현하고싶었다.
    localField만 검색 시에는 문제없이 find 메소드로 잘 되었으나, 연결된 외부 필드를 적용 시 찾질 못했다.
    populate를 쓰고싶어 가능한 find로 이리저리 시도해보았으나, 계속 $regex 에러가 떳다.
    결국 aggregate로 변경했고 오늘 처음 $lookup과 $unwind를 사용했다..
    $lookup이 바로 populate처럼 두 컬렉션을 연결시켜주는 역할이었다.
    $lookup 사용으로 작품 모델과 작가 모델을 합쳐주었다.
  • from: 연결하고자하는 컬랙션명
  • localField: 현재 컬랙션에서 해당 컬랙션과 연결되는 필드명
  • foreignField: 해당 컬랙션에서 로컬과 연결되는 필드명
  • as: 연결된 문서를 받을 필드명(내가 만들어도 되고 로컬필드명을 써도 된다)
  • pipeline: 조인할 컬렉션에서 실행하고자 하는 것을 쓰는데 아직 덜 숙지되었다.
  • let: 파이프라인과 추후 더 숙지해야할 것!
  • 문서 연결 후, 페이지네이션을 위한 총 문서 갯수 오류
    구글링을 통해 aggregate로 문서를 연결했을 시, $count 사용법을 찾았다.
    문서를 연결할 때와 똑같은 로직을 두번 써야하는 것이 껄끄러웠지만 다른 방법을 못 찾았다.
    $count가 $group + $project와 똑같은 기능이었다...!!
  • aggregate 시, $sort / $limit / $skip이 되지않는 오류
    $limit과 $skip의 위치문제였는데 아직도 명확하게 이해하지는 못 했다.
    아마 파이프라인대로 정보 가공 시에 문제가 생긴 것 같다.
const pageSize = 28;

  const products = await Product.aggregate([
    { $match: { inStock: true } },
    {
      $lookup: {
        from: 'artists',
        localField: 'artist',
        foreignField: '_id',
        as: 'artist',
      },
    },
    {
      $unwind: '$artist',
    },
    { $match: { ...keyword } },
    {
      $project: {
        'artist.code': 0,
        'artist.record': 0,
        'artist.thumbnail': 0,
        'artist.likes': 0,
        'artist.countOfWorks': 0,
        'artist.isActive': 0,
        'artist.joinAt': 0,
      },
    },
    { $sort: { _id: -1 } },
    { $skip: pageSize * (page - 1) },
    { $limit: pageSize },
  ]);

  const count = await Product.aggregate([
    { $match: { inStock: true } },
    {
      $lookup: {
        from: 'artists',
        localField: 'artist',
        foreignField: '_id',
        as: 'artist',
      },
    },
    { $match: { ...keyword } },
    { $count: 'of_products' },
  ]);

  res.json({
    products,
    page,
    pages: Math.ceil(count[0].of_products / pageSize),
  });

내일은 프로젝트에 기여하기 위해 무엇을 해야 하나요?

  • 배포 자동화 업데이트 후, 체크
  • 버그 잡아내기
  • 개인 기술 발표 구상하기
@heartane heartane added the dev-log dev 로그 작성시 사용하는 라벨 label Dec 17, 2021
@heartane heartane self-assigned this Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev-log dev 로그 작성시 사용하는 라벨
Projects
None yet
Development

No branches or pull requests

1 participant