[英]Find difference between two dates picked from jQuery datepicker
[英]Mongoose find results between 2 dates from html datepicker
我有以下子文檔:
{
id: 1,
date:2019-04-01 00:21:19.000
},
{
id: 2,
date:2019-03-31 00:21:19.000
} ...
Document schema is :
const barEventSchema = new Schema({
id: {
type: Number,
unique: true,
required: true
},
raw: { type: String },
date: { type: Date },
type: { type: String },
})
const FooSchema = new Schema({
bar: [barEventSchema]
})
我想根據從HTML輸入,具有象值挑選日期范圍做了查詢2019-04-01
, 2019-03-31
。 因此,在服務器端,我想執行以下操作:
//@star_date = 2019-04-01, @end_date = 2019-04-01
Foo.findOne('bar.date' : {$lte : start_date, $gte: end_date})
但是,這將返回所有文檔。
可以使用以下方法檢索所有子文檔的日期在開始和結束日期范圍之間的子文檔:
const conditions = {
'bar': {
$elemMatch: {
'date': {
$gte: new Date(start_date),
$lte: new Date(end_date)
}
}
}
}
Foo.find(conditions)
如果存在至少一個子文檔,且其日期在condition
指定的范圍之間,則將返回所有文檔。 $elemMatch
運算符用於在bar子文檔的date
字段上實現此條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.