簡體   English   中英

需要使用 Mongo DB 找出給定日期的值

[英]Need to find out the value for a given date by using Mongo DB

{
_id:"id-1",
createdDate:new Date("2020-01-01"),
"eventH":[{
    date: new Date("2020-01-01"),
    attribute1: value,
    attribute2: "value"
},
{
    date: new Date("2020-02-01"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-02-20"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-02-28"),
    attribute1:value,
    attribute1:"value"
},
{
    date: new Date("2020-03-05"),
    attribute1: value,
    attribute2:"value"
}]
},
{
_id:"id-2",
createdDate:new Date("2020-01-02"),
eventH:[{
    date: new Date("2020-01-02"),
    attribute1: value,
    attribute2: "value"
},
{
    date: new Date("2020-02-01"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-02-20"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-02-28"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-03-05"),
    attribute1: value,
    attribute2:"value"
}]
},
{
_id:"id-3",
createdDate:new Date("2020-01-02"),
eventH:[{
    date: new Date("2020-01-02"),
    attribute1: value,
    attribute2: "value"
},
{
    date: new Date("2020-02-01"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-02-20"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-02-28"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-03-05"),
    attribute1: value,
    attribute2:"value"
}]
},
{
_id:"id-4",
createdDate:new Date("2020-01-02"),
eventH:[{
    date: new Date("2020-01-02"),
    attribute1: value,
    attribute2: "value"
},
{
    date: new Date("2020-02-01"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-02-20"),
    attribute1:value,
    attribute2:"value"
},
{
    date: new Date("2020-02-28"),
    attribute1: value,
    attribute2: "value"
},
{
    date: new Date("2020-03-05"),
    attribute1: value,
    attribute2:"value"
}]
}

假設以上是數據庫中可供我使用的架構。 目標:對於給定的日期,找出屬性 1 和屬性 2 的值,如果它與我要查找的內容匹配,則不計算在內。 請注意,eventH 是一個數組,其中包含在一段時間內更改的屬性值。

例如:截至 2020 年 3 月 5 日,給我屬性 1 = value1 和屬性 2 = value2 的文檔,我想直接在 Mongodb 處理它。 雖然我可以使用 pandas 來處理它,但是對於更大的數據集和更大的 eventH object,會有延遲。如果我錯了,請糾正我。

日期可以是 2020 年 3 月 29 日的 ex 的任何值,id-1 的最后一個值將是 2020 年 3 月 5 日的值。或者 id-1 的 2020 年 2 月 27 日的值將與 2 月 20 日的值相同2020 年,因為在 2020 年 2 月 20 日之后,值在 2020 年 2 月 28 日發生了變化,所以如果我查詢 2020 年 2 月 28 日,它應該給我截至該日期的值。


我認為以下查詢是您正在尋找的。

db.test2.aggregate([
    {
        "$project": {
            "eventH": {
                "$filter": {
                    "input": "$eventH",
                    "as": "arrayElem",
                    "cond": {
                        "$and": [
                            {"$eq": ["$$arrayElem.date", ISODate("2020-01-02T05:30:00.000+05:30")]},
                            {"$eq": ["$$arrayElem.attribute1", "value"]},
                            {"$eq": ["$$arrayElem.attribute2", "value"]},
                        ]
                    }
                }
            }
        }
    },
    {
        "$match": {
            "eventH": {"$ne": []}
        }
    }
])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM