我试图在深层嵌套数组会话中推送新对象。如何将新数据推入这样的数组?我的Mongoose Schema是:
{
month: String,
sessionsCount: Number,
dates: [{
day: String,
sessionsCount: Number,
activeUsers: [{
userId: String,
userName: String,
userEmail: String,
lastActivityDate: Date,
sessions: [{
// some fields
}]
}]
}]
}
查询更新:
let query = {
"month": date.month,
"dates.day": date.day,
"dates.activeUsers.userId": req.body.userId
};
let updateOptions = {
$push: {"dates.activeUsers.sessions": data.dates.activeUsers.sessions}
}
Telemetry.update(query, updateOptions).then(session => res.json(session)).catch(error => res.json(error));
错误:
{
"name": "MongoError",
"message": "cannot use the part (dates of dates.activeUsers.sessions) to traverse the element ({dates: [ { day: \"22nd Jan\", _id: ObjectId('59520b5702bf00dc3482ceaf'), activeUsers: [ { userId: \"58f13c41114a70384a877b2e\", userName: \"Joel\", userEmail: \"jwasinger@thengan.com\", lastActivityDate: new Date(1485089983421), _id: ObjectId('59520b5702bf00dc3482ceb0'), sessions: [ { sessionStartDate: new Date(1496988833666), calendarShowChanged: 0, navigationDateChanged: 0, viewChanged: 0, orientationChanged: 0, multiboardChanged: 0, settingsChanged: 0, cardsRefreshed: 0, cardCreated: 0, cardRemoved: 0, cardEdited: 22, cardDropped: 0, cardOpened: 0, shortcutActivated: 0, sessionId: \"2\", appType: \"chromeExtension\", _id: ObjectId('59520b5702bf00dc3482ceb1') } ] } ], sessionsCount: 1 } ]})"
}
更新嵌套数组中的数据时出现同样的问题