這里是MongoDB檔案。
[
{
name: "a"/span>,
"start_date": ISODate("2021-09-21T12:13:34.151 05:30"),
"end_date"。ISODate("2021-09-24T12:13:34.000 05:30"),
},
{
name: "b",
"start_date": ISODate("2021-09-21T12:13:34.151 05:30"),
"end_date"。ISODate("2021-09-22T12:13:34.000 05:30"),
},
{
name: "c"。
"start_date": ISODate("2021-09-21T12:13:34.151 05:30"),
"end_date"。ISODate("2021-09-21T12:13:34.000 05:30"),
}
]
我想根據開始日期和結束日期來獲取所有資料。
我嘗試了以下方法。
我嘗試了以下方法。
db.collection.find({
start_date: {
$gte: new Date()
},
end_date: {
$lte: new Date()
}
})
預期輸出
如果當前日期是2021-09-22,那么輸出將是
[
{
name: "a"/span>,
"start_date": ISODate("2021-09-21T12:13:34.151 05:30"),
"end_date"。ISODate("2021-09-24T12:13:34.000 05:30"),
},
{
name: "b",
"start_date": ISODate("2021-09-21T12:13:34.151 05:30"),
"end_date"。ISODate("2021-09-22T12:13:34.000 05:30"),
},
]
如果當前日期是2021-09-23,那么輸出將是
[
{
name: "a"/span>,
"start_date": ISODate("2021-09-21T12:13:34.151 05:30"),
"end_date"。ISODate("2021-09-24T12:13:34.000 05:30"),
},
]
uj5u.com熱心網友回復:
請按照下面提到的邏輯來改變所需的輸出
。db.collection.find({
start_date: {
$lte: new Date()
},
end_date: {
$gte: new Date()
}
})
uj5u.com熱心網友回復:
使用$and進行多重表達。
注意:start_date小于($lte)當前日期和end_date大于($gte)當前日期,以便當前日期在日期范圍內。
db.collection.find({
$and: [
{
start_date: {
$lte: new Date()
}
},
{
end_date: {
$gte: new Date()
}
}
]
})
OR
db.collection.find({
"$expr"/span>: {
"$and": [
{
$lte: [
"$start_date",
new Date( )
]
},
{
$gte: [
"$end_date"/span>,
new Date( )
]
}
]
}
})
uj5u.com熱心網友回復:
// With NodeJs
Collection.find( {
$and: [
{
start_date: {
$gte: new Date().toISOString()。
},
},
{
end_date: {
$lte: new Date().toISOString()。
},
},
],
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/331060.html
標籤:
下一篇:N應該被分割成多少個數字的總和
