๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring Data MongoDB Array field $elemMatch(MongoRepository Custom)

by ์ฃผ๋ฐœ2 2022. 8. 20.
๋ฐ˜์‘ํ˜•

๐Ÿ“Ž MongoRepository Custom ๋ฐ $elemMatch ์‚ฌ์šฉํ•˜๊ธฐ

์ตœ๊ทผ ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ MongoDB๋ฅผ ์ฒ˜์Œ์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•œ ๋กœ์ง์€ Query Method๋‚˜ @Query ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๊ฐ„๋‹จํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์œผ๋‚˜, MongoDB์˜ ๊ฒฝ์šฐ ๋ฌธ๋ฒ•์— ์ต์ˆ™์น˜ ์•Š๋‹ค๋ณด๋‹ˆ ์ง์ ‘ Repository๋ฅผ ์ปค์Šคํ…€์œผ๋กœ ์ƒ์„ฑํ•ด์„œ ๊ด€๋ฆฌํ•˜๋Š”๊ฒŒ ๋ณด๋‹ค ํŽธ๋ฆฌํ•  ๋“ฏ ํ•˜์—ฌ ์ปค์Šคํ…€ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

 

์ถ”๊ฐ€์ ์œผ๋กœ ์•ฝ๊ฐ„ ์‚ฝ์งˆ์„ํ–ˆ๋˜ Query ๋ฐ Criteria, $elemMatch๋ฅผ ํ†ตํ•ด MongoDB์—์„œ Array field๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋„ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฌผ๋ก  QueryDSL์„ ์ ์šฉํ•˜๋ฉด ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์œผ๋‚˜, ์ถ”ํ›„ ๋ณ€๊ฒฝํ•˜๋„๋ก ํ•˜๊ณ  ์ด๋ฒˆ์—๋Š” Custom Repository๋งŒ ์ƒ์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

(ํฌ์ŠคํŒ…์— ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ํ”ผ๋“œ๋ฐฑ ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜ƒ)

 

 

 

๐Ÿ“Œ  ๋ถ„์„

ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉ์ค‘์ธ Bookmark Entity์˜ ํ•„๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

(์•„๋ฌด๋ž˜๋„ NoSQL์˜ ํŠน์„ฑ ์ƒ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ” ๋ฐ ํ•˜๋‚˜์˜ ์ฟผ๋ฆฌ๋กœ ์—ฌ๋Ÿฌ๊ฐ€์ง€๋“ค์„ ํ•ด๊ฒฐํ•˜๋ ค๋‹ค ํ•˜๋‹ค๋ณด๋‹ˆ Big Table์ด ๋œ ๋“ฏ ํ•œ ๋Š๋‚Œ์ž…๋‹ˆ๋‹ค..๐Ÿ˜‚)

reimnd๋Š” Array ํ˜•์‹์œผ๋กœ ๋‚ด๋ถ€์— userId, remindTime, fcmToken, remindCheck, remindStatus ํ•„๋“œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

์œ„ ํ•„๋“œ์—์„œ remind์˜ ๊ฒฝ์šฐ ๊ธฐ์กด์—๋Š” Bookmark์™€ 1:1 ๋งคํ•‘์ด์—ˆ์ง€๋งŒ, ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋ฉด์„œ Bookmark์™€ Remind์˜ ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ 1:N์ด ๋˜์—ˆ๊ณ , ์ด๋กœ ์ธํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณ ๋ฏผ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.

  • Embedding vs Referencing

์ „์ž์˜ ๊ฒฝ์šฐ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋“  ๊ฒƒ์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ˜๋ฉด, ํ›„์ž์˜ ๊ฒฝ์šฐ ์ผ๋ฐ˜์ ์œผ๋กœ RDBMS์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”๊ฐ„ JOIN์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ €๋Š” Embedding๋ฅผ ์„ ํƒํ•˜์—ฌ Remind์˜ ๊ฒฝ์šฐ Array๋กœ ๊ด€๋ฆฌํ•˜๊ณ ์ž ํ–ˆ๋Š”๋ฐ์š”, ์•„๋ž˜์—์„œ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์ง€๋งŒ ๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” ๋‹จ์ผ ์ฟผ๋ฆฌ๋กœ ๋ชจ๋“  ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์–ด ์„ฑ๋Šฅ์ƒ์— ์ด์ ์ด ์กด์žฌํ•œ๋‹ค๊ณ  ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

MongoDB Document์—์„œ ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ์— ๋Œ€ํ•ด ํ•ด๊ฒฐ์•ˆ์„ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”, ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

Embedding

Advantages

  1. ๋‹จ์ผ ์ฟผ๋ฆฌ๋กœ ๋ชจ๋“  ๊ด€๋ จ๋œ ์ •๋ณด๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ์— joins์ด๋‚˜ $lookup๊ณผ ๊ฐ™์€ ์ฝ”๋“œ ๊ตฌํ˜„์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ๋‹จ์ผ ์›์ž์„ฑ์˜ ์ž‘์—…์œผ๋กœ ๊ด€๋ จ ์ •๋ณด๋“ค์„ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  4. ๋ฉ€ํ‹ฐ ์ž‘์—…์œผ๋กœ ์ธํ•œ ํŠธ๋žœ์žญ์…˜์ด ํ•„์š”ํ•˜๋‹ค๋ฉด, transaction operator๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Limitations

  1. ๋งŒ์•ฝ ๋งŽ์€ ํ•„๋“œ๊ฐ€ ์—ฐ๊ด€์„ฑ์ด ์—†๋‹ค๋ฉด, documents๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋” ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  2. MongoDB์—๋Š” 16MB์˜ Document Size ์ œํ•œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๋‹จ์ผ documen์— ๋„ˆ๋ฌด ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๊ฒฝ์šฐ ์ž ์žฌ์ ์œผ๋กœ ์ด๋Ÿฌํ•œ ์ œํ•œ์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Referencing

Advantages

  1. ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆ„๊ธฐ์— ๋ณด๋‹ค ์ž‘์€ documents๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ 16MB Documnet Size ์ œํ•œ์— ๋„๋‹ฌํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋‚ฎ์Šต๋‹ˆ๋‹ค.
  3. ์ž์ฃผ ์ ‘๊ทผํ•˜์ง€ ์•Š๋Š” ์ •๋ณด๋Š” ์ฟผ๋ฆฌ์— ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  4. ๋ฐ์ดํ„ฐ ์ค‘๋ณต์ด ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค.

Limitations

  1. ๊ด€๋ จ documents์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ์ตœ์†Œ ๋‘ ๋ฒˆ์˜ ์ฟผ๋ฆฌ ํ˜น์€ $lookup์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์ €๋Š” I/O Call์„ ์ค„์ด๊ณ ์ž Embedding์„ ์‚ฌ์šฉํ–ˆ๊ณ , ๊ฒฐ๊ตญ ์ •๋‹ต์€ ์—†๋Š” ํŠธ๋ ˆ์ด๋“œ ์˜คํ”„์ด๊ธฐ์— ์ƒํ™ฉ์— ๋งž๋Š” ์„ ํƒ์„ ํ•˜๋ฉด ๋  ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.

ํ˜น์‹œ ๋น„์Šทํ•œ ์ƒํ™ฉ์ด์‹œ๋ผ๋ฉด ์œ„์— ๋งํฌ๋กœ ์ฒจ๋ถ€ํ•ด๋†“์•˜๋˜ MongoDB Document์— ์ž์„ธํžˆ ์„ค๋ช…์ด ๋‚˜์™€์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!

 

 

 

๐Ÿ“Œ  ๋ฌธ์ œ

ํ˜„์žฌ Documents์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํ˜•์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์œ„์—์„œ remindList Array ๋‚ด์—์„œ userId(+remindCheck & remindStatus)์˜ ์กฐ๊ฑด์— ๋”ฐ๋ผ ํ•ด๋‹น ์›์†Œ๋งŒ ๊ฐ€์ ธ์™€์•ผ ํ–ˆ๋Š”๋ฐ์š”, 

๊ธฐ์กด์˜ Query Method๋‚˜ @Query์˜ ๋ฐฉ์‹์œผ๋กœ๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ userId๊ฐ€ ํฌํ•จํ•˜๋Š” ๋ชจ๋“  ์›์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ๋“ค์–ด Array๋‚ด์— userId = 26์ธ ์ฟผ๋ฆฌ๋ฅผ ์กฐํšŒํ•˜๋ฉด 0๋ฒˆ์งธ ์›์†Œ์ธ ๋นจ๊ฐ„์ƒ‰ ๋„ค๋ชจ๋ฐ•์Šค๋งŒ ํ•„์š”ํ•˜์ง€๋งŒ, ์‹ค์ œ ๊ฒฐ๊ณผ๋Š” ํŒŒ๋ž€์ƒ‰ ๋„ค๋ชจ๋ฐ•์Šค์ธ ์ „์ฒด๊ฐ€ ๋‚˜์˜ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ์š”, ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    @Query(value = "{\$and: [{remindList: {\$elemMatch: {userId: ?0}}}, {remindList: {\$elemMatch: {remindCheck: false}}}, {remindList: {\$elemMatch: {remindCheck: false}}} ]}")
    fun findAllBookmarkByUserIds(userId: Long): List<Bookmark>

์ฟผ๋ฆฌ์—.. ์›Œ๋‚™ ๋งŽ์€ ๊ด„ํ˜ธ๋“ค์ด ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋‹ˆ ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ธฐ์— MongoRepository๋ฅผ Customํ•ด์„œ ๊ตฌํ˜„ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

(์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ’์ด ์•ˆ๋‚˜์˜จ๊ฑด ์•„๋ฌด๋ž˜๋‘ ์ฟผ๋ฆฌ์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๋“ฏ ํ•œ๋ฐ ์ž์„ธํžˆ๋Š” ๋ชจ๋ฅด๊ฒ ๋„ค์š”.. ๐Ÿ˜ญ)

 

 

 

๐Ÿ“Œ  ๊ตฌ์กฐ

๊ตฌ์กฐ๋Š” ๋งค์šฐ ๋‹จ์ˆœํ•œ๋ฐ์š” ๊ธฐ์กด์— MongoRepository๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ์‚ฌ์šฉ์ค‘์ธ BookmarkRepository ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์กด์žฌํ•˜๊ณ , ์ถ”๊ฐ€์ ์œผ๋กœ Customํ•˜๊ณ ์ž ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค(MongoTemplateRepository)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค(BookmarkRepositoryImpl)์„ ์ƒ์„ฑํ•œ ๋’ค ๊ธฐ์กด์˜ BookmarkRepository ์ธํ„ฐํŽ˜์ด์Šค์—์„œ MongoTemplateRepository ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

(์ฐธ๊ณ ๋กœ ๊ตฌํ˜„ ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ Impl์œผ๋กœ ๋๋‚˜์•ผ ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค..;)

 

 

๊ตฌํ˜„ ์ฝ”๋“œ

interface MongoTemplateRepository {

    fun findAllRemindByUserId(userId: Long): List<Bookmark>
}


import com.yapp.web2.domain.bookmark.entity.Bookmark
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query

class BookmarkRepositoryImpl(
    private val mongoTemplate: MongoTemplate
) : MongoTemplateRepository {

    // userId ๊ธฐ์ค€์œผ๋กœ ๋ฆฌ๋งˆ์ธ๋“œ ๋ฐœ์†ก๋˜์—ˆ์œผ๋ฉฐ ๋ฏธํ™•์ธ์ธ ๋ถ๋งˆํฌ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ
    override fun findAllRemindByUserId(userId: Long): List<Bookmark> {
        val query: Query = Query()

        query.addCriteria(
            Criteria.where("remindList").elemMatch(
                Criteria.where("userId").`is`(userId)
                    .and("remindCheck").`is`(false)
                    .and("remindStatus").`is`(true)
            )
        )

        query.fields().include("userId")
        query.fields().include("folderId")
        query.fields().include("link")
        query.fields().include("title")
        query.fields().include("remindList.$")

        return mongoTemplate.find(query, Bookmark::class.java)
    }

}


interface BookmarkRepository : MongoRepository<Bookmark, String>, MongoTemplateRepository {
...
}

 

์ฝ”๋“œ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ตฌํ˜„์ฒด์ธ BookmarkRepositoryImpl ํด๋ž˜์Šค์—๋Š” MongoTemplate์„ ์ฃผ์ž…๋ฐ›๋„๋ก ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

(๋น„์Šทํ•œ ๊ฐœ๋…์œผ๋กœ MongoOperations์ด ์žˆ์Šต๋‹ˆ๋‹ค. MongoTemplate๋Š” ํด๋ž˜์Šค, MongoOperations๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฉฐ MongoTemplate์ด MongoOperations์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. MongoTemplate - MongoOperations )

 

๊ทธ ํ›„ Query ๋ฐ Criteria๋ฅผ ํ†ตํ•ด ์กฐ๊ฑด์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ์š”, elemMatch๋Š” Array์—์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

($elemMatch์˜ ๊ฒฝ์šฐ ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ผ๊ฒฝ์šฐ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋งŒ ๋ฆฌํ„ดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋“ค์„ ์ฐพ์œผ๋ ค๋ฉด Aggregation Operations ์„ ์‚ฌ์šฉํ•ด์„œ ์ž‘์—…์„ ํ•ด์•ผ ํ•˜๋Š” ๋“ฏ ํ•œ๋ฐ ํ•ด๋ณด์ง€๋Š” ์•Š์•„์„œ ์ž์„ธํžˆ๋Š” ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค ^^; - ์ฐธ๊ณ  ๋งํฌ

์ €์˜ ๊ฒฝ์šฐ userId๊ฐ€ ์ค‘๋ณต๋  ์ผ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณต์ˆ˜ ๊ฐœ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์—†๊ธฐ์— $elemMatch๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๐Ÿ˜ญ)

 

query().fields().include() ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” find ๊ฒฐ๊ณผ๊ฐ’์„ @Document๊ฐ€ ์„ค์ •๋œ Entity์— ๋งคํ•‘๋  ๋•Œ ๋งคํ•‘์‹œํ‚ฌ ํ•„๋“œ๋ฅผ ํฌํ•จํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”, ํ•„์š”ํ•œ ๊ฐ’์— ๋Œ€ํ•ด์„œ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด null์ด ๋“ค์–ด์˜ค๋‹ˆ ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

userId, folderId, link ํ•„๋“œ๋Š” Bookmark Entity์—์„œ ์ƒ์„ฑ์ž๋กœ ํ•„์š”ํ•œ ๊ฐ’์ด๋ผ ์„ค์ •์„ ํ•ด์ฃผ์—ˆ๊ณ , title์€ ์‚ฌ์šฉ๋  ๊ฐ’์ด์–ด์„œ ์„ค์ •์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋ผ๋ฉด @PersistenceConstructor ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด Entity๋กœ ๋งคํ•‘๋  ๋•Œ ์„ ํƒํ•  ์ƒ์„ฑ์ž๋ฅผ ์ง์ ‘ ์„ ํƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  • @PersistenceConstructor: Marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved Document.

 

 

๐Ÿ“Œ  ๊ฒฐ๊ณผ

์œ„ ๊ตฌํ˜„ ์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•ด๋ณด๋ฉด ์‹คํ–‰๋˜๋Š” ์ฟผ๋ฆฌ ๋ฐ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

find using query: { "remindList" : { "$elemMatch" : { "userId" : 26, "remindCheck" : false, "remindStatus" : true}}} fields: Document{{remindList.$=1, link=1, title=1, userId=1, folderId=1}}

ํฌ์ŠคํŠธ๋งจ์„ ํ†ตํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๋ฉด userId, remindCheck, remindStatus ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” Array์˜ ์›์†Œ๋งŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋‹ค๋ฅธ ์ฝ”๋“œ์—์„œ๋„ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์•ˆ๋‚˜์˜ค๋Š” ์ฟผ๋ฆฌ๊ฐ€ ์žˆ๋Š”๋ฐ, ์–ธ๋Šฅ ์ˆ˜์ •ํ•˜๋Ÿฌ ๊ฐ€์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿœ

 

 

 

๐Ÿ“Œ  ์ฐธ๊ณ ๋ฌธ์„œ

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€