将元素推入嵌套数组mongoose nodejs
Push element into nested array mongoose nodejs
我正在尝试将一个新元素放入数组中,我在基于 express/nodejs 的 api 上使用猫鼬。这是猫鼬的代码:
Serie.updateOne({'seasons.episodes.videos._id': data._id}, {$push: {'seasons.episodes.videos.$.reports': data.details}},
function(err) {
if (err) {
res.status(500).send()
console.log(err)
}
else res.status(200).send()
})
我的系列模型是这样的:
const serieSchema = new mongoose.Schema({
name: {type: String, unique:true, index: true, text: true},
customID: {type: Number, required: true, unique: true, index: true},
featured: {type: Boolean, default: false, index: true},
seasons: [{
number: Number,
episodes: [{number: Number, videos: [
{
_id: ObjectId,
provider: String,
reports: [{
title: {type: String, required: true},
description: String
}],
quality: {type: String, index: true, lowercase: true},
language: {type: String, index: true, lowercase: true},
}
]}]
}],
});
当我执行我的代码时,我得到 MongoDB 错误代码 16837,即 "cannot use the part (seasons of seasons.episodes.videos.0.reports) to traverse the element (my element here on json)"
我尝试了很多其他查询来解决这个问题,但 none 成功了,我希望有人能解决这个问题。
在您的查询中,您正在使用 位置运算符($ 符号)通过 _id 对一个特定视频进行本地化,然后您想将一个项目推送到报告中。
问题是 MongoDB 不知道您要更新哪个视频,因为您指定的路径 (seasons.episodes.videos.$.reports) 包含另外两个数组(季节和剧集)。
如文档所述,您不能多次使用此运算符
The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value
此限制使您的情况复杂化。您仍然可以更新您的报告,但您需要知道外部数组的确切索引。所以以下更新将是工作示例:
db.movies.update({'seasons.episodes.videos._id': data._id}, {$push: {'seasons.0.episodes.0.videos.$.reports': data.details}})
或者,您可以在 node.js 中更新本文档的大部分内容,或者在牢记技术限制的情况下重新考虑您的架构设计。
我正在尝试将一个新元素放入数组中,我在基于 express/nodejs 的 api 上使用猫鼬。这是猫鼬的代码:
Serie.updateOne({'seasons.episodes.videos._id': data._id}, {$push: {'seasons.episodes.videos.$.reports': data.details}},
function(err) {
if (err) {
res.status(500).send()
console.log(err)
}
else res.status(200).send()
})
我的系列模型是这样的:
const serieSchema = new mongoose.Schema({
name: {type: String, unique:true, index: true, text: true},
customID: {type: Number, required: true, unique: true, index: true},
featured: {type: Boolean, default: false, index: true},
seasons: [{
number: Number,
episodes: [{number: Number, videos: [
{
_id: ObjectId,
provider: String,
reports: [{
title: {type: String, required: true},
description: String
}],
quality: {type: String, index: true, lowercase: true},
language: {type: String, index: true, lowercase: true},
}
]}]
}],
});
当我执行我的代码时,我得到 MongoDB 错误代码 16837,即 "cannot use the part (seasons of seasons.episodes.videos.0.reports) to traverse the element (my element here on json)"
我尝试了很多其他查询来解决这个问题,但 none 成功了,我希望有人能解决这个问题。
在您的查询中,您正在使用 位置运算符($ 符号)通过 _id 对一个特定视频进行本地化,然后您想将一个项目推送到报告中。
问题是 MongoDB 不知道您要更新哪个视频,因为您指定的路径 (seasons.episodes.videos.$.reports) 包含另外两个数组(季节和剧集)。
如文档所述,您不能多次使用此运算符
The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value
此限制使您的情况复杂化。您仍然可以更新您的报告,但您需要知道外部数组的确切索引。所以以下更新将是工作示例:
db.movies.update({'seasons.episodes.videos._id': data._id}, {$push: {'seasons.0.episodes.0.videos.$.reports': data.details}})
或者,您可以在 node.js 中更新本文档的大部分内容,或者在牢记技术限制的情况下重新考虑您的架构设计。