' $中的日期比较'查询未给出预期结果

时间:2014-08-17 18:46:54

标签: mongodb

我正在尝试比较'$ where'查询中的日期来过滤数据。我的'where'查询看起来像这样:

function () {
    var messageStatusInfoList = this.messageStatusInfoList
    var startDate = someDate;
    var result = false;
    for (var counter = 0; counter < messageStatusInfoList.length; counter++) {
        var currentMessageStatusInfo = messageStatusInfoList[counter]
        if (counter > 0 && (currentMessageStatusInfo.messageStatus == "RESPONDED" )) {
            var responseDate = currentMessageStatusInfo.effectiveDate
            if(+responseDate >= +startDate) {
                result = true;
                break;
            }
        }
    }
    return result;
}

以下是示例输入文档:

{
    "_id" : NumberLong(3687),
            "messageStatusInfoList" : [
            {
                    "effectiveDate" : ISODate("2014-08-01T13:29:26.456Z"),
                    "expirationDate" : ISODate("2014-08-04T11:40:29.824Z"),
                    "messageStatus" : "OPENED"
            },
            {
                    "effectiveDate" : ISODate("2014-08-04T11:40:29.824Z"),
                    "expirationDate" : ISODate("2014-08-05T13:01:00.135Z"),
                    "messageStatus" : "RESPONDED",
                    "userId" : NumberLong(8)
            },
            {
                    "effectiveDate" : ISODate("2014-08-05T13:01:00.135Z"),
                    "messageStatus" : "REPLY_TO_CUSTOMER",
                    "userId" : NumberLong(8)
            }
    ],
    "tenantId" : NumberLong(4),
    "text" : "some text ..",
    "version" : NumberLong(12)
}

根据上述文档并给出查询的位置,如果startDate设置为2014-08-03,则日期比较应评估为true(因为responseDate为2014-08-04T11:40:29.824Z且responseDate更大比startDate)。但事实并非如此。比较运算符的计算结果为false。

1 个答案:

答案 0 :(得分:0)

问题是由于我的where查询中的变量startDate初始化不正确。