有沒有辦法在 mongo 資料庫中搜索集合的多個范圍?我嘗試了以下但不起作用雖然它適用于單個范圍
db.collection(collection)
.aggregate([
{
$search: {
compound: {
filter: [
{
range: {
path: createdAt,
gte: startdate,
lte: endDate,
},
},
{
range: {
path: updatedAt,
gte: startdate,
lte: endDate,
},
},
],
},
},
},
{
$limit:20,
},
])
.toArray()
uj5u.com熱心網友回復:
你可以簡單地把$or在$expr在$match您的聚集階段
db.collection.aggregate([
{
"$match": {
$expr: {
$or: [
{
$and: [
{
$gte: [
"$createdAt",
startDate
]
},
{
$lte: [
"$createdAt",
endDate
]
}
]
},
{
$and: [
{
$gte: [
"$updatedAt",
startDate
]
},
{
$lte: [
"$updatedAt",
endDate
]
}
]
}
]
}
}
}
])
這是Mongo 游樂場供您參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335960.html
標籤:javascript 数据库 MongoDB 搜索 mongodb-图集
