개발/DB

🌱mongoDB:: Array로 find?

hyuunii 2022. 11. 30. 18:07

이상한(?)걸 발견했다.

 

게시글 북마크 기능 구현 중 북마크 스키마 자체를 없애고 유저 안의 칼럼으로 쌓이도록 해달라는 요청을 받았다.

 

services/users.js

getBookmark = async(nickName) => {
  let result = []
  const getBookmark = await this.usersRepository.getBookmark(nickName);
  console.log("getBookmark", getBookmark)  //여기서는 nickName에 해당하는 유저의 정보 찾아와짐
  const GetBookmark = getBookmark.map((userInfo) => userInfo.bookmark)  //위의 유저정보에서 bookmark만 map
  console.log("GetBookmark", GetBookmark)

  for (let i = 0; i < GetBookmark.length; i++){
    console.log(GetBookmark[0])
    const AllgetBookmark = await this.usersRepository.AllgetBookmark(GetBookmark[i])
    console.log("AllgetBookmark", AllgetBookmark)

    if(AllgetBookmark.length === 0) {
      const err = new Error('postsService Error');
      err.status = 200
      err.message = "등록된 게시물이 없습니다."
      throw err
    } else if(AllgetBookmark.length !== 0){
      result.push(AllgetBookmark)
    }
    console.log(AllgetBookmark)
  }

  return result
}

console;

getBookmark [
  {
    _id: new ObjectId("63861f839e4f7e314551bf7f"),
    userId: 'jade',
    nickName: 'jade',
    password: '$2a$11$IrBvbE6kQ1D618G8N2E9Le9OygALEoXnFbiehnKY5wq48jf0FIgBe',
    phoneNumber: '01074628335',
    address: '제주특별자치도 서귀포시 가가로 14',
    myPlace: [ '제주특별자치도', '서귀포시' ],
    age: '28',
    gender: 'female',
    visible: true,
    likeGame: [],
    userAvater: { Eye: 1, Hair: 1, Mouth: 1, Back: 1 },
    point: 200,
    totalPoint: 200,
    createdAt: 2022-11-29T15:04:35.893Z,
    updatedAt: 2022-11-29T15:04:35.894Z,
    __v: 0,
    refresh_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2Njk3ODkzODMsImV4cCI6MTk3Njg1NDk4M30.1SHD2D8mVtcqFyYcclV8wphKo9g9HIvMfvuUQIByLCc',
    bookmark: [ '6386245b9e4f7e314551c053', '6386212f9e4f7e314551c01a' ]
  }
]


GetBookmark [ [ '6386245b9e4f7e314551c053', '6386212f9e4f7e314551c01a' ] ]


GetBookmark[0] [ '6386245b9e4f7e314551c053', '6386212f9e4f7e314551c01a' ]

 

GetBookmark.length = 1

 

그니까 저 배열([ '6386245b9e4f7e314551c053', '6386212f9e4f7e314551c01a' ]) 상태로 repo로 넘어간다.

 

AllgetBookmark [
  {
    _id: new ObjectId("6386212f9e4f7e314551c01a"),
    userId: 'qqqq',
    nickName: 'qqqq',
    title: '날짜 테스ㅡ',
    content: '날짜날짜',
    location: { x: '127.122496940127', y: '37.4897427415779' },
    cafe: '서울 송파구 동남로 99',
    date: '임시',
    time: [ '2022-11-30T15:00:00.000Z', '2022-12-01T14:00:00.000Z' ],
    map: '송파구',
    partyMember: 3,
    participant: [ '1234' ],
    confirmMember: [ 'qqqq' ],
    banUser: [],
    closed: 0,
    expireAt: 2022-12-01T14:00:00.000Z,
    createdAt: 2022-11-29T15:11:43.297Z,
    updatedAt: 2022-11-29T15:11:43.297Z,
    __v: 0
  },
  {
    _id: new ObjectId("6386245b9e4f7e314551c053"),
    userId: 'qqqq',
    nickName: 'qqqq',
    title: '되나??',
    content: '이게 되나?',
    location: { x: '127.034757121285', y: '37.4849665053325' },
    cafe: '서울 강남구 강남대로 238',
    date: '임시',
    time: [ '2022-11-29T15:00:00.000Z', '2022-11-30T14:00:00.000Z' ],
    map: '강남구',
    partyMember: 10,
    participant: [],
    confirmMember: [ 'qqqq' ],
    banUser: [],
    closed: 0,
    expireAt: 2022-11-30T14:00:00.000Z,
    createdAt: 2022-11-29T15:25:15.742Z,
    updatedAt: 2022-11-29T15:25:15.742Z,
    __v: 0
  }
]

 

 

 

repositories/users.js

AllgetBookmark = async(postId) => {
    console.log("postId", postId)
    const AllgetBookmark = await Posts.find({_id:postId});
    console.log("repo-AllgetBookmark", AllgetBookmark)
    return AllgetBookmark
  }

 

console;

postId [ '6386245b9e4f7e314551c053', '6386212f9e4f7e314551c01a' ]

배열로 잘 넘어왔는데

 

배열로 잘 찾아진다 ........???????????

배열 그대로 Posts에서 postId값으로 잘 찾아서 배열로 다시 잘 뱉어준다.

몽고디비는.... 신기하다........

 

repo-AllgetBookmark [
  {
    _id: new ObjectId("6386212f9e4f7e314551c01a"),
    userId: 'qqqq',
    nickName: 'qqqq',
    title: '날짜 테스ㅡ',
    content: '날짜날짜',
    location: { x: '127.122496940127', y: '37.4897427415779' },
    cafe: '서울 송파구 동남로 99',
    date: '임시',
    time: [ '2022-11-30T15:00:00.000Z', '2022-12-01T14:00:00.000Z' ],
    map: '송파구',
    partyMember: 3,
    participant: [ '1234' ],
    confirmMember: [ 'qqqq' ],
    banUser: [],
    closed: 0,
    expireAt: 2022-12-01T14:00:00.000Z,
    createdAt: 2022-11-29T15:11:43.297Z,
    updatedAt: 2022-11-29T15:11:43.297Z,
    __v: 0
  },
  {
    _id: new ObjectId("6386245b9e4f7e314551c053"),
    userId: 'qqqq',
    nickName: 'qqqq',
    title: '되나??',
    content: '이게 되나?',
    location: { x: '127.034757121285', y: '37.4849665053325' },
    cafe: '서울 강남구 강남대로 238',
    date: '임시',
    time: [ '2022-11-29T15:00:00.000Z', '2022-11-30T14:00:00.000Z' ],
    map: '강남구',
    partyMember: 10,
    participant: [],
    confirmMember: [ 'qqqq' ],
    banUser: [],
    closed: 0,
    expireAt: 2022-11-30T14:00:00.000Z,
    createdAt: 2022-11-29T15:25:15.742Z,
    updatedAt: 2022-11-29T15:25:15.742Z,
    __v: 0
  }
]