gpt4 book ai didi

mongodb - 如何在 Mongoose 中选择具有开始和结束日期的数据范围

转载 作者:可可西里 更新时间:2023-11-01 10:02:14 25 4
gpt4 key购买 nike

我有包含开始日期和结束日期的数据列表,我想根据给定的日期范围获取选定的数据。

/* 1 */
{
"_id" : ObjectId("5a10dfd83aa9c689cb838e4d"),
"name" : "R1",
"startDate" : ISODate("2017-11-17T20:58:57.191Z"),
"endDate" : ISODate("2017-11-20T20:58:57.191Z")
}

/* 2 */
{
"_id" : ObjectId("5a10e0093aa9c689cb838e8e"),
"name" : "R2",
"startDate" : ISODate("2017-11-10T20:58:57.191Z"),
"endDate" : ISODate("2017-11-25T20:58:57.191Z")
}

/* 3 */
{
"_id" : ObjectId("5a10e02e3aa9c689cb838ec4"),
"name" : "R3",
"startDate" : ISODate("2017-11-25T20:58:57.191Z"),
"endDate" : ISODate("2017-11-27T20:58:57.191Z")
}

/* 4 */
{
"_id" : ObjectId("5a10e06c3aa9c689cb838f18"),
"name" : "R4",
"startDate" : ISODate("2017-11-15T20:58:57.191Z"),
"endDate" : ISODate("2017-11-18T20:58:57.191Z")
}

如果获取开始日期-“2017-11-16”和结束日期“2017-11-21”,那么它应该选择 R1、R2 和 R4,请帮我实现这个 Mongoose 查询

最佳答案

您可以使用以下查询来获得此结果,这可能会有所帮助。

db.getCollection.find({ $or: [
{ $and: [{"startDate": {"$lte":ISODate("2017-11-16T20:58:57.191Z")}},{ "endDate": {"$gte": ISODate("2017-11-21T20:58:57.191Z") }}]},
{ $and: [{"startDate": {"$lte":ISODate("2017-11-16T20:58:57.191Z")}},{ "endDate": {"$gte": ISODate("2017-11-16T20:58:57.191Z") }}]},
{ $and: [{"startDate": {"$lte":ISODate("2017-11-21T20:58:57.191Z")}},{ "endDate": {"$gte": ISODate("2017-11-21T20:58:57.191Z") }}]},
{ $and: [{"startDate": {"$gte":ISODate("2017-11-16T20:58:57.191Z")}},{ "endDate": {"$lte": ISODate("2017-11-21T20:58:57.191Z") }}]}
]})

关于mongodb - 如何在 Mongoose 中选择具有开始和结束日期的数据范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47373207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com