[英]MongoDB Get Single Object Nested in Array of Objects in Array of Objects
您如何基於一些屬性值(嵌套在偽代碼中)來訪問嵌套在對象數組中的單個對象,該對象嵌套在另一個對象數組中
SELECT DAY = 1 WHERE _id = 5a3469f22dc3784bdd9a6190 AND MONTH = 12
貓鼬模型架構如下所示。 根據需要,列出的子文檔高於其相應的父文檔,dailySchedulesSchema是最高的:
var dailySchedulesSchema = new mongoose.Schema({
day: Number,
dayStart: Number,
firstBreakStart: Number,
firstBreakEnd: Number,
lunchStart: Number,
lunchEnd: Number,
secondBreakStart: Number,
secondBreakEnd: Number,
dayEnd: Number,
workDuration: Number
});
var monthlyScheduleSchema = new mongoose.Schema({
month: {type: Number, required: true },
dailySchedules: [dailySchedulesSchema]
});
var employeeSchema = new mongoose.Schema({
name: {type: String, required: true},
surname: {type: String, required: true},
email: {type: String, required: true},
phone: {type: String, required: true},
occupation: {type: String, required: true},
status: {type: Boolean, required: true},
monthlySchedule: [monthlyScheduleSchema]
});
這是我要處理的數據庫中的雇員條目。
"_id" : ObjectId("5a3469f22dc3784bdd9a6190"),
"name" : "Eric",
"surname" : "K. Farrell",
"email" : "EricKFarrell@dayrep.com",
"phone" : "864-506-7281",
"occupation" : "Employee",
"status" : true,
"monthlySchedule" : [
{
"month" : 12,
"dailySchedules" : [
{
"day" : 1,
"dayStart" : 480,
"firstBreakStart" : 600,
"firstBreakEnd" : 615,
"lunchStart" : 720,
"lunchEnd" : 750,
"secondBreakStart" : 870,
"secondBreakEnd" : 885,
"dayEnd" : 1020,
"workDuration" : 480
},
{
"day" : 2,
"dayStart" : 540,
"firstBreakStart" : 630,
"firstBreakEnd" : 645,
"lunchStart" : 750,
"lunchEnd" : 780,
"secondBreakStart" : 870,
"secondBreakEnd" : 885,
"dayEnd" : 1050,
"workDuration" : 480
}
]
}
]
}
獲得單日的路線本身是: “ / employees /:employeeid /:month /:day” 。
當我設法訪問父文檔(如列出所有員工)時,我無法列出特定的子文檔條目(如該員工的特定日程安排)-貓鼬要么返回了當月的所有現有日程安排,要么沒有返回任何所有:
(......)
var sendJsonResponse = function(res, status, content){
res.status(status);
res.json(content);
}
(......)
module.exports.empDayReadOne = function(req, res){
var monthParam = req.params.month;
var employeeid = req.params.employeeid;
var dayParam = req.params.day;
Emp
.aggregate([
{$match: {$and: [{'monthlySchedule': {$elemMatch: {$exists: true} } }, {_id: employeeid }] } },
{$unwind:'$monthlySchedule'},
{$unwind:'$monthlySchedule.dailySchedules'},
{$match:{ $and:[ {'monthlySchedule.dailySchedules.day': dayParam},{'monthlySchedule.month': monthParam} ] } }
])
.exec(function(err, dailySchedule){
if(dailySchedule){
sendJsonResponse(res, 200, dailySchedule);
} else if(err){
sendJsonResponse(res, 400, err);
return;
} else {
sendJsonResponse(res, 404, {"message": "This day has no schedules added."});
return;
}
});
};
您可以嘗試下面的聚合查詢。
下面的查詢首先查詢$filters
monthlySchedule
,該查詢返回具有匹配月份數組元素的數組,然后返回第二個過濾器以檢索dailySchedules
數組中匹配的日期元素。
$arrayElemAt
將具有單個元素的數組轉換為文檔。
db.collection_name.aggregate([
{"$match":{"_id":ObjectId("5a3469f22dc3784bdd9a6190")}},
{"$project":{
"dailyschedule":{
"$arrayElemAt":[
{"$filter":{
"input":{
"$filter":{
"input":"$monthlySchedule",
"cond":{"$eq":["$$this.month",12]}
}
},
"cond":{"$eq":["$$this.day",1]
}
}},
0]
}
}}
])
更新的貓鼬代碼:
Emp.aggregate([
{"$match":{"_id":mongoose.Types.ObjectId(employeeid)}},
{"$project":{
"dailyschedule":{
"$arrayElemAt":[
{"$filter":{
"input":{
"$filter":{
"input":"$monthlySchedule",
"cond":{"$eq":["$$this.month",monthParam]}
}
},
"cond":{"$eq":["$$this.day",dayParam]
}
}},
0]
}
}}
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.