基于mongoDB中的当前日期从和到日期之间获取数据

jdzmm42g  于 2023-06-29  发布在  Go
关注(0)|答案(1)|浏览(125)

我在MongoDB数据库中有一个集合,其中有一些数据,并希望查询数据。基于boardId、title,其中当前日期介于开始日期和结束日期之间,并且结束日期比当前日期过期。我想拿的那些文件。
样本数据

{
    "_id": {
        "$oid": "6464c0c6f51f03d917106612"
    },
    "id": 4401800840,
    "boardId": 4401799576,
    "name": "Distribute Branded Swag to Site Crew(s)",
    "columnValues": [
        {
            "id": "timeline_1",
            "title": "Actual Timeline",
            "value": "{\"to\":\"2023-08-07\",\"from\":\"2023-08-01\",\"changed_at\":\"2023-05-02T15:15:45.669Z\"}"
        }
    ],
    "parentId": null,
    "groupName": "Construction",
},
{
    "_id": {
        "$oid": "6464c0c6f51f03d917106616"
    },
    "id": 4401800551,
    "boardId": 4401799576,
    "name": "2 Week Lookahead Schedule-test",
    "columnValues": [
        {
            "id": "timeline_1",
            "title": "Actual Timeline",
            "value": "{\"to\":\"2023-05-06\",\"from\":\"2023-03 31\",\"changed_at\":\"2023-05-03T12:10:00.565Z\"}"
        }
    ],
    "parentId": null,
    "groupName": "Construction"
},
{
    "_id": {
        "$oid": "6464c0c6f51f03d917106617"
    },
    "id": 4401800726,
    "boardId": 4401799576,
    "name": "2 Week Lookahead Schedule",
    "columnValues": [
        {
            "id": "timeline_1",
            "title": "Actual Timeline",
            "value": "{\"to\":\"2023-06-30\",\"from\":\"2023-03-27\",\"changed_at\":\"2023-05-02T15:15:45.609Z\"}"
        }
    ],
    "parentId": null,
    "groupName": "Construction",
}

我使用下面的命令来获取数据,当前日期介于从日期和到日期之间,但它没有给予我任何结果。不知道我错过了什么。请指导

const currentDate = new Date();
const currentDates = currentDate.toISOString().split('T')[0];
const queryItem = {
  boardId: { $in: boardIds },
  groupName: "Construction",
  columnValues: {
    $elemMatch: {
      title: "Actual Timeline",
      value: {
        $elemMatch: {
          from: { $lt: currentDates },
          to: { $gt: currentDates }
        }
      }
    }
  }
};
const constructionItems = await Items.find(queryItem).exec();
62lalag4

62lalag41#

您在value中提供的数据似乎格式不正确。数据应该是这样的:

{
"_id": {
  "$oid": "6464c0c6f51f03d917106612"
},
"id": 4401800840,
"boardId": 4401799576,
"name": "Distribute Branded Swag to Site Crew(s)",
"columnValues": [
  {
    "id": "timeline_1",
    "title": "Actual Timeline",
    "value": {
        "to":"2023-12-12",
        "from":"2023-01-1",
        "changed_at":"2023-05-03T12:10:00.565Z"
    }
  }
],
"parentId": null,
"groupName": "Construction"
},
{
"_id": {
  "$oid": "6464c0c6f51f03d917106616"
},
"id": 4401800551,
"boardId": 4401799576,
"name": "2 Week Lookahead Schedule-test",
"columnValues": [
  {
    "id": "timeline_1",
    "title": "Actual Timeline",
    "value": {
        "to":"2023-05-06",
        "from":"2023-03-31",
        "changed_at":"2023-05-03T12:10:00.565Z"
    }
  }
],
"parentId": null,
"groupName": "Construction"
},
{
"_id": {
  "$oid": "6464c0c6f51f03d917106617"
},
"id": 4401800726,
"boardId": 4401799576,
"name": "2 Week Lookahead Schedule",
"columnValues": [
  {
    "id": "timeline_1",
    "title": "Actual Timeline",
    "value":{
        "to":"2023-05-06",
        "from":"2023-03-31",
        "changed_at":"2023-05-03T12:10:00.565Z"
    }
  }
],
"parentId": null,
"groupName": "Construction"
}

然后将queryItem修改为:

const queryItem = {
    boardId: {$in: 4401799576},
    groupName: "Construction",
    "columnValues.title": "Actual Timeline",
    "columnValues.value.from": { $lt: currentDates },
    "columnValues.value.to": { $gt: currentDates },
};

这给出如下输出:

Connected to MongoDB
[
  {
    _id: new ObjectId("6464c0c6f51f03d917106612"),
    id: 4401800840,
    boardId: 4401799576,
    name: 'Distribute Branded Swag to Site Crew(s)',
    columnValues: [ [Object] ],
    parentId: null,
    groupName: 'Construction'
  }
]

希望这个能帮上忙

相关问题