如何查询同一天和小时中有20个或更多文档的日期

发布于 2025-01-21 13:26:12 字数 965 浏览 0 评论 0原文

我有一个像这样的蒙古多德数据库结构,

{
  _id: ObjectId,
  name: string,
  scheduledDate: ISOString
}

我想返回所有在所有数据库中重复2次或以上的计划日期,

示例:

{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-15T05:44:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-15T07:44:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-18T02:44:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-18T02:20:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-18T02:44:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-10T05:44:00.000Z"
}

在此示例2022-04-15中,重复2次和2022-04-18重复3次,因此两者都符合标准(2次或更多),所以我想返回两个日期 这可能吗?

这样:

{
  scheduledDate:"2022-04-15T00:00:00.000Z"
},
{
  scheduledDate:"2022-04-18T00:00:00.000Z"
}

还有一个问题,可以在小时数中做同样的事情吗?在所有数据库x times中重复的计划日期的特定小时列表

I have a mongoDB database structure like this

{
  _id: ObjectId,
  name: string,
  scheduledDate: ISOString
}

I want to return all scheduledDates that repeat the same scheduledDate day 2 times or more across all the database

Example:

{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-15T05:44:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-15T07:44:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-18T02:44:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-18T02:20:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-18T02:44:00.000Z"
},
{
  _id: ObjectId,
  name: 'example1',
  scheduledDate: "2022-04-10T05:44:00.000Z"
}

In this example 2022-04-15 repeats 2 times and 2022-04-18 repeat 3 times, so both match the criteria (2 times or more) so I want to return both date day
Is this possible?

Like this:

{
  scheduledDate:"2022-04-15T00:00:00.000Z"
},
{
  scheduledDate:"2022-04-18T00:00:00.000Z"
}

And one more question, is possible to do the same with hours? A list of specific hours of scheduledDate that repeat across all database X times

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

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

发布评论

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

评论(1

请叫√我孤独 2025-01-28 13:26:12

使用$ group带有$ date$ datetunc

db.collection.aggregate([
  {
    $group: {
      _id: {
        $dateTrunc: {
          date: { $toDate: "$scheduledDate" },
          unit: "day"
        }
      },
      count: { $sum: 1 }
    }
  },
  {
    $match: {
      count: { $gt: 1 }
    }
  },
  {
    $project: {
      _id: 0,
      scheduledDate: "$_id"
    }
  }
])

mongoplayground

Use $group with $date and $dateTrunc

db.collection.aggregate([
  {
    $group: {
      _id: {
        $dateTrunc: {
          date: { $toDate: "$scheduledDate" },
          unit: "day"
        }
      },
      count: { $sum: 1 }
    }
  },
  {
    $match: {
      count: { $gt: 1 }
    }
  },
  {
    $project: {
      _id: 0,
      scheduledDate: "$_id"
    }
  }
])

mongoplayground

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