Node.js 在MongoDB文档集合中查找对象数组中的元素
我将Nodejs后端与MongoDB一起使用,以Mongoose作为ORM保存我的对象结构。Node.js 在MongoDB文档集合中查找对象数组中的元素,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我将Nodejs后端与MongoDB一起使用,以Mongoose作为ORM保存我的对象结构。 我有一个工单对象,它遵循以下模式。生成的工单有数千份,我需要查找在给定日期分配给给定员工的工单。 你能帮我提出一个查找问题吗 查询详细信息 应该能够查询和查找给定工人在给定日期的年度分配工单详细信息 需要使用allocatedWorker(worker)和allocatedDateTime(日期和时间)来匹配和查找 工单对象 { customer: String, addre
我有一个工单对象,它遵循以下模式。生成的工单有数千份,我需要查找在给定日期分配给给定员工的工单。
你能帮我提出一个查找问题吗 查询详细信息
allocatedWorker
(worker)和allocatedDateTime
(日期和时间)来匹配和查找工单
对象{
customer: String,
address: String,
/**
* List of to do items. Each item will mainly belong in to a given category
* and a collection of tasks.
* The task can be system generated, admin assigned or customer raised.
*/
todo: [
{
category: String,
/**
* A collection of raised tasks
*/
tasks: [
{
/**
* Holds the customer request details
*/
request: {
workItem: String,
instructions: String,
/**
* Preferred date and time
*/
dateTime: String,
/**
* Preferred worker if any
*/
preferredWorker: String,
receivedDateTime: String
},
/**
* Details of the schedule
*/
schedule: [
{
/**
* Allocated worker's user name
*/
allocatedWorker: String,
allocatedDateTime: String,
allocatedDuration: Number,
/**
* Schedule changed date and time
*/
scheduledOn: String,
/**
* Scheduler's user name
*/
scheduledBy: String,
/**
* Scheduled task status
* 0: Pending - new
* 1: Scheduled - allocated
* 2: Work In Progress
* 3: Completed
* 4: Overdue
*/
itemStatus: Number
}
],
/**
* Stage of the work order
* 0: Pending - new
* 1: Scheduled - allocated
* 2: Work In Progress
* 3: Completed
* 4: Overdue
*/
status: Number
}
]
}
]
}
请尝试以下查询:
Workorder.find({'todo.tasks.schedule.allocatedWorker': worker,
'todo.tasks.schedule.allocatedDateTime': day}, function(err, orders) {});
请记住,两者都将进行存储在db中的精确值匹配(精确名称和日期时间)。这是我尝试过的,
Workorder.find({$and:[{'todo.tasks.schedule.allocatedWorker':worker},{'todo.tasks.schedule.allocatedDateTime':day}]},函数(err,orders){}