我必須創建一個 API,其端點類似于 day=1&time=1000 并且通過使用這些查詢引數我必須撰寫一個 mongo 查詢,JSON 就像
"_id" : ObjectId("62257ddd76b35400010e7015"),
"applyOffersOn" : [
{
"day" : 1,
"startTime" : NumberLong("1646625639561"),
"endTime" : NumberLong("1646631039561"),
"startTimeFormat" : NumberLong(930),
"endTimeFormat" : NumberLong(1100)
},
{
"day" : 2,
"startTime" : NumberLong("1646625639561"),
"endTime" : NumberLong("1646631039561"),
"startTimeFormat" : NumberLong(930),
"endTimeFormat" : NumberLong(1100)
},
{
"day" : 3,
"startTime" : NumberLong("1646625639561"),
"endTime" : NumberLong("1646631039561"),
"startTimeFormat" : NumberLong(1930),
"endTimeFormat" : NumberLong(2100)
}
],
}```
the query should be like I have to first check for a particular object using objectId then finding the object, check for the day is present as 1 and then check for the time that it will be between
startTimeFormat and endTimeFormat if all these conditions satisfy then return the whole object
I have written the query but it either works for the day or for a time but it should work like it check for both condition
uj5u.com熱心網友回復:
我認為這會有所幫助,
##elemMatch - 它將回圈遍歷陣列的所有元素。
const answer = <your model schema>.findById(req.params.id).find({applyOffersOn:{$elemMatch:{day:<your day from query> } }}).find({applyOffersOn:{$elemMatch : {startTimeFormat: {$lte:<your time from query> },endTimeFormat:{$gte:<your time from query>} }}})
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/440160.html
下一篇:我想使用API在我的顫振應用程式中使用convertapi.com將word檔案轉換為pdf檔案,我的POST方法有什么問題
