[英]Mongo aggregation; match where a field's subfield has at least one array element greater than specified value
[英]Is there a way to query documents in mongoDB/mongoose to match ALL objects in an array property WHERE the datetime is greater than a specified date
const CourseSchema = new Schema({
course_name: {
type: String,
trim: true,
},
Course_Runs: [
{
run_id: {
type: String,
},
// This field is for Teachers that are involved in the course run
teachers: [{ type: Schema.Types.ObjectId, ref: "users" }],
// Course Date Duration will be an inferred field from first session and last session
Sessions: [{
session_start_datetime: {
type: Date,
},
session_end_datetime: {
type: Date,
},
]
}]
}
以上是架构的片段
我希望能够获取所有会话 object 的 session_start_datetime 大于指定日期(例如当前日期)的文档。
YOURMODEL.find( { Sessions: { $all: [{session_start_datetime: {$gte: YOUR_DATE}] } } )
不要忘记以正确的日期格式传递 YOUR_DATE,否则它将不起作用。
例如,如果您的日期是一个字符串:
YOURMODEL.find( { Sessions: { $all: [{session_start_datetime: {$gte: new Date(YOUR_DATE)}] } } )
尝试:
model.find({
'Course_Runs.Sessions.session_start_datetime': {
$gt: current_date
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.