【问题标题】:$ifNull operator is not working in my mongo db FIND query for date key fields$ifNull 运算符在我的 mongo db FIND 查询中无法使用日期键字段
【发布时间】:2019-10-04 21:43:36
【问题描述】:

我想提取所有在 NVL(修改日期,创建日期)之后具有给定日期的订单。

modifiedDateTime 和 createdDateTime 不遵循大多数其他日期字段,我似乎无法弄清楚如何对它们进行操作符。 并非所有记录都有 modifiedDateTime,所以当它丢失时,我想退回到 createDateTime。 .我尝试了一些东西,但无法获得任何数据逻辑来测试它。

  • 在以下位置查找所有记录:
  • 示例值:2018 年 8 月 24 日星期五 23:25:16 UTC
  • 如果 modifiedDateTime 存在:日期 > modifiedDateTime
  • 如果 modifiedDateTime 不存在:日期 > createdDateTime
  • SQL 版本:WHERE NVL(modifiedDateTime, createdDateTime) > '30-SEP-2019'

我尝试了以下但没有成功:

db.getCollection('createProvisionServiceOrderRepositoryRequest').find(
    {
        'modifiedDateTime': {
            '$ifNull': [
                {'$gte': new ISODate("2019-09-30T00:00:00.000Z")},
                {'createdDateTime': {'$gte': new ISODate("2019-09-30T00:00:00.000Z")}}
            ]
        }
    }
).count()

两个文档示例:

{
        "_id" : "CANO-857098021127729",
        "_class" : "yyyy",
        "customerOrderNumber" : "xxxx",
        "billingAccountNumber" : "123",
        "version" : "1",
        "customerServiceOrderStatus" : "REJECTED",
        "createdDateTime" : "Mon Sep 04 17:35:41 IST 2017"
}
{
        "_id" : "CUMO-971416171118821",
        "_class" : "yyyy",
        "customerOrderNumber" : "xxxx",
        "provisioningServiceOrder" : [
                {
                        "_id" : "string",
                        "provisioningServiceOrderObject" : "Test Data",
                        "status" : "string",
                        "correctionList" : [
                                {
                                        "correctionType" : "string",
                                        "correctionName" : "string",
                                        "correctionAction" : "string",
                                        "correctionOldValue" : "string",
                                        "correctionNewValue" : "string",
                                        "attributeList" : [
                                                {
                                                        "attributeType" : "string",
                                                        "attributeName" : "string",
                                                        "attributeAction" : "string",
                                                        "attributeOldValue" : "string",
                                                        "attributeNewValue" : "string"
                                                }
                                        ]
                                }
                        ]
                }
        ],
        "customerServiceOrderStatus" : "completed",
        "modifiedDateTime" : "Wed Sep 06 07:15:47 UTC 2017"
}

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    我们可以简单地在modifiedDateTimecreatedDateTime 检查上应用 OR。如果modifiedDateTime 为空或小于指定日期,则仅评估 OR 块中的第二个条件。

    我们可以使用$toDate 运算符将正确的日期字符串转换为日期对象。

    例如:

    db.collection.find({
        $expr:{
            $or:[
                {
                    $gte:[
                        {
                            $toDate:"$modifiedDateTime"
                        },
                        new ISODate("2017-09-03T00:00:00.000Z")
                    ]
                },
                {
                    $gte:[
                        {
                            $toDate:"$createdDateTime"
                        },
                        new ISODate("2017-09-03T00:00:00.000Z")
                    ]
                }
            ]
        }
    }).pretty()
    

    对于 MongoDB 3.4:

    db.collection.find({
        $where: function(){
            var selectedDate = new ISODate("2017-09-03T00:00:00.000Z");  
            if(this.modifiedDateTime == null){
                if(this.createdDateTime != null){
                    var preparedCreatedDateTime = this.createdDateTime.substring(0,11) + this.createdDateTime.substring(24) + " " + this.createdDateTime.substring(11,19);
                    return new Date(preparedCreatedDateTime) > selectedDate;
                }
            }else{
                var preparedModifiedDateTime = this.modifiedDateTime.substring(0,11) + this.modifiedDateTime.substring(24) + " " + this.modifiedDateTime.substring(11,19);
                return new Date(preparedModifiedDateTime) > selectedDate;
            }
            return false;
        }
    }).pretty()
    

    数据集:

    {
        "_id" : "CANO-857098021127729",
        "_class" : "yyyy",
        "customerOrderNumber" : "xxxx",
        "customerServiceOrderStatus" : "REJECTED",
        "createdDateTime" : "Mon Sep 04 17:35:41 IST 2017"
    }
    {
        "_id" : "CUMO-971416171118821",
        "_class" : "yyyy",
        "customerOrderNumber" : "xxxx",
        "customerServiceOrderStatus" : "completed",
        "modifiedDateTime" : "Wed Sep 06 07:15:47 UTC 2017"
    }
    {
        "_id" : "CUMO-971416171118824",
        "_class" : "yyyy",
        "customerOrderNumber" : "xxxx",
        "customerServiceOrderStatus" : "completed",
        "modifiedDateTime" : "Sat Sep 02 07:15:47 UTC 2017"
    }
    

    输出:

    {
        "_id" : "CANO-857098021127729",
        "_class" : "yyyy",
        "customerOrderNumber" : "xxxx",
        "customerServiceOrderStatus" : "REJECTED",
        "createdDateTime" : "Mon Sep 04 17:35:41 IST 2017"
    }
    {
        "_id" : "CUMO-971416171118821",
        "_class" : "yyyy",
        "customerOrderNumber" : "xxxx",
        "customerServiceOrderStatus" : "completed",
        "modifiedDateTime" : "Wed Sep 06 07:15:47 UTC 2017"
    }
    

    【讨论】:

    • 当我尝试这个查询得到以下错误:错误:错误:{“ok”:0,“errmsg”:“未知顶级运算符:$expr”,“code”:2,“ codeName" : "BadValue"
    • 您可以将 $expr 与 mongo 版本 3.6+ 一起使用。
    • 3.4-12 mongo percona
    • 你能帮我如何获取3.4-12版本的查询
    • 当然。一段时间后会恢复。
    猜你喜欢
    • 1970-01-01
    • 2017-03-25
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2012-12-02
    • 2019-05-13
    相关资源
    最近更新 更多