通过比较Mongo DB中的田间日期来获取行

发布于 2025-02-13 21:10:33 字数 1525 浏览 2 评论 0原文

我试图比较两个集合本身以获取行,但无法获得行。

    [{
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
        "date_modified" : ISODate("2022-06-29T05:01:15.370+0000"),
    },
    {
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
        "date_modified" : ISODate("2022-06-19T05:01:15.370+0000"),
    },
    {
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"), 
    }]

查询

    db.getCollection("collection_name").aggregate([ 
                 {
                    $match: {
                     status: '1',
                          $or: [
                              {
                                  date_modified: { $gt: ISODate('$date_created') } 
                              },
                              { 
                                 date_modified: {
                                     "$exists": false,
                                },
                               },
                           ], 
                    }, 
                },
    ]);

预期结果:

[{
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
        "date_modified" : ISODate("2022-06-29T05:01:15.370+0000"),
    },{
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"), 
    }]

当前结果: date_created尚未定义

I am trying to compare two fields of collection mongo itself to get rows, but not able to get it.

    [{
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
        "date_modified" : ISODate("2022-06-29T05:01:15.370+0000"),
    },
    {
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
        "date_modified" : ISODate("2022-06-19T05:01:15.370+0000"),
    },
    {
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"), 
    }]

Query

    db.getCollection("collection_name").aggregate([ 
                 {
                    $match: {
                     status: '1',
                          $or: [
                              {
                                  date_modified: { $gt: ISODate('$date_created') } 
                              },
                              { 
                                 date_modified: {
                                     "$exists": false,
                                },
                               },
                           ], 
                    }, 
                },
    ]);

Expected result:

[{
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
        "date_modified" : ISODate("2022-06-29T05:01:15.370+0000"),
    },{
        "date_created" : ISODate("2022-06-24T05:01:15.370+0000"), 
    }]

Current result: date_created is not defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝戈者 2025-02-20 21:10:33

您需要使用$ expr进行字段之间的比较。使用$ ifnull来满足您检查date_created字段的存在的要求。

db.collection.find({
  $expr: {
    $or: [
      {
        $eq: [
          {
            $ifNull: [
              "$date_modified",
              null
            ]
          },
          null
        ]
      },
      {
        $gt: [
          "$date_modified",
          "$date_created"
        ]
      }
    ]
  }
})

这是 mongo playground 供您参考。

You need to use $expr for comparison between fields. Use $ifNull to cater your requirement for checking the existence for the date_created field.

db.collection.find({
  $expr: {
    $or: [
      {
        $eq: [
          {
            $ifNull: [
              "$date_modified",
              null
            ]
          },
          null
        ]
      },
      {
        $gt: [
          "$date_modified",
          "$date_created"
        ]
      }
    ]
  }
})

Here is the Mongo Playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文