如何在猫鼬中通过Funcion找到数据?

发布于 2025-02-08 14:14:04 字数 1599 浏览 0 评论 0原文

我在mongodb中使用猫鼬包在下面的菜单中有一个集合

[
 {
    _id: new ObjectId("62ae97b6be08b688f93f2c07"),
    reportId: '1',
    method: 'A1',
    category: 'B2',
    date: '2022-06-19',
    time: '22:55',
    emergency: 'normal',
    __v: 0
  },
  {
    _id: new ObjectId("62ae97b6be08b688f93f2c08"),
    reportId: '2',
    method: 'A3',
    category: 'B5',
    date: '2022-06-18',
    time: '23:05',
    emergency: 'normal',
    __v: 0
  },
  {
    _id: new ObjectId("62ae97b6be08b688f93f2c09"),
    reportId: '3',
    method: 'A5',
    category: 'B1',
    date: '2022-06-19',
    time: '23:55',
    emergency: 'urgent',
    __v: 0
  }
]

,我想过滤这些数据,这是我的find function()

const options = [
 { method: { $in: ['A1','A2'] } },
 { emergency: { $in: data.emergency } },
 { category: { $in: data.category } }
];

const response = await Report.find({ $or: options,});

到目前为止,它运行得很好,但是我仍然有一个过滤器:datetime(它们都是int类型字符串)。

我想在今晚23点到23点之后的昨晚之间搜索范围日期和时间。

但是我不知道如何编写查询,请帮助我弄清楚,谢谢!!!

这是我的测试查询:

 date: {
       $where: function () {
              const yesterday = moment().subtract(1, 'days').format('YYYYMMDD') + '2300';
              const date = moment(this.date).format('YYYYMMDD') + this.time.replace(':', '');
              const today = moment().format('YYYYMMDD') + '2300';
              return yesterday < date && date <= today;
            },
          },

I have a collection in mongodb using mongoose packages like below

[
 {
    _id: new ObjectId("62ae97b6be08b688f93f2c07"),
    reportId: '1',
    method: 'A1',
    category: 'B2',
    date: '2022-06-19',
    time: '22:55',
    emergency: 'normal',
    __v: 0
  },
  {
    _id: new ObjectId("62ae97b6be08b688f93f2c08"),
    reportId: '2',
    method: 'A3',
    category: 'B5',
    date: '2022-06-18',
    time: '23:05',
    emergency: 'normal',
    __v: 0
  },
  {
    _id: new ObjectId("62ae97b6be08b688f93f2c09"),
    reportId: '3',
    method: 'A5',
    category: 'B1',
    date: '2022-06-19',
    time: '23:55',
    emergency: 'urgent',
    __v: 0
  }
]

and I want to filter this data, and here is my find function()

const options = [
 { method: { $in: ['A1','A2'] } },
 { emergency: { $in: data.emergency } },
 { category: { $in: data.category } }
];

const response = await Report.find({ $or: options,});

Until now, it works perfectly, but I still got one more filter: the date and time (They are all int type String).

I want to search for the range date and time between last night after 23 o'clock to 23 o'clock tonight.

But I have no idea how to write the query, please help me figure it out, thanks!!!

Here is my testing query:

 date: {
       $where: function () {
              const yesterday = moment().subtract(1, 'days').format('YYYYMMDD') + '2300';
              const date = moment(this.date).format('YYYYMMDD') + this.time.replace(':', '');
              const today = moment().format('YYYYMMDD') + '2300';
              return yesterday < date && date <= today;
            },
          },

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

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

发布评论

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

评论(1

娇纵 2025-02-15 14:14:04

我建议您首先以支持比较器(LT,GT等)的格式节省日期,例如时间戳甚至平均数字。这样,您可以使用这样的MongoDB过滤器:

let filter = {
  date: { $gt: new Date(..), $lt: new Date(..) }
}

// This can be executed directly in the Mongo shell.
{
  created_on: {
    $gt: ISODate("1972-12-12"),
    $lt: ISODate('2022-06-019')
  }
}

但是,如果您坚持使用字符串,则需要过滤所有文档,然后分析每个文档以进行比较。

I would recommend you to first save dates in a format that supports comparators (lt, gt, etc.) such as a Timestamp or even a plain number. That way you can use MongoDB filters like this:

let filter = {
  date: { $gt: new Date(..), $lt: new Date(..) }
}

// This can be executed directly in the Mongo shell.
{
  created_on: {
    $gt: ISODate("1972-12-12"),
    $lt: ISODate('2022-06-019')
  }
}

But if you insist on using strings, then you would need to filter all your documents, and then parse each one to do your comparison.

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