programing

$ 또는 $elemMatch의 문

lovejava 2023. 7. 13. 20:13

$ 또는 $elemMatch의 문

할 수 있는 방법이 있습니까?$or의 진술.$elemMatch?

나는 다음을 가지고 있습니다.

opened: {
    $elemMatch: {
        closed: false
        openingEvening: {$lte: currentTime}, 
        closingEvening: {$gte: currentTime},
    }
}

그리고 추가하고 싶습니다.openingMorning끝까지

다음과 같이 확장할 수 있는 방법:

opened: {
    $elemMatch: {
        closed: false
        {$or: [
            {openingEvening: {$lte: currentTime}, closingEvening: {$gte: currentTime},},
            {openingMorning: {$lte: currentTime}, closingMorning: {$gte: currentTime}}
         ]
        }
    }
}

이것과 같은 것이 가능합니까?

물론, 구문을 정리하는 것뿐이지만 기본적으로 사용할 수 있습니다.

{
    "opened": {
        "$elemMatch": {
            "closed": false,
            "$or": [
                {
                    "openingEvening": { "$lte": currentTime  },
                    "closingEvening": { "$gte": currentTime  }
                },
                {
                    "openingMorning": { "$lte": currentTime },
                    "closingMorning": { "$gte": currentTime  }
                }

            ]
        }
    }
}

그리고 데이터에 대한 샘플 아이디어가 주어지면,

{
    "_id" : ObjectId("537969cee90c3db84958aa86"),
    "opened" : [
            {
                    "closed" : false,
                    "openingEvening" : 17,
                    "closingEvening" : 22,
                    "openingMorning" : 11,
                    "closingMorning" : 14
            }
    ]
}
{
    "_id" : ObjectId("53796a47e90c3db84958aa87"),
    "opened" : [
            {
                    "closed" : false,
                    "openingMorning" : 13,
                    "closingMorning" : 14
            }
    ]
}

의 현재 시간12첫 번째 문서와 일치하지만 두 번째 문서는 일치하지 않지만 값은 일치합니다.13둘 다 일치할 것입니다.

또한 이러한 항목이 배열 내에 있으므로 예상 목적을 고려할 때 "dayOfWeek" 필드도 여기에 포함하기를 원할 수 있습니다.

언급URL : https://stackoverflow.com/questions/23726927/or-statement-in-elemmatch