【发布时间】:2014-02-26 11:31:58
【问题描述】:
我已经定义了这个架构:
var bookCollection = new mongoose.Schema({
book:[{
bookTitle: String,
bookOrder: Number,
bookChapters: [{
chapterTitle: String,
chapterIntro: String,
chapterOrder: Number,
chapterArticles: [{
articleTitle: String,
articleIntro: String,
articleOrder: Number,
articleHeadings: [{
headingTitle: String,
headingOrder: Number
}]
}]
}]
}]
});
var bookModel = mongoose.model('bookModel', bookCollection);
然后我将 1 个文档保存到 mongoDB,这是使用 db.bookmodels.find() 检查时的 JSON 对象
{
"_id": ObjectId("530cc92710f774355434b394"),
"book": [
{
"bookTitle": "Javascript",
"bookOrder": 300,
"_id": ObjectId("530cc92710f774355434b395"),
"bookChapters": [
{
"chapterTitle": "Functions",
"chapterIntro": "All about javascript functions",
"chapterOrder": 500,
"_id": ObjectId("530cc92710f774355434b396"),
"chapterArticles": [
{
"articleTitle": "A visual illustration of the JS function",
"articleIntro": "Something to see here, check it out",
"articleOrder": 500,
"_id": ObjectId("530cc92710f774355434b397"),
"articleHeadings": [
{
"headingTitle": "Parts of a function",
"headingOrder": 500,
"_id": ObjectId("530cc92710f774355434b398")
}
]
}
]
}
]
}
],
"__v": 0
}
如果我想将 headingOrder 更改为 100 而不是 500,我将如何使用 mongoose.js 更新数据库?我一直在尝试几件事,但我似乎无法理解它。
在任何地方,您都会看到具有简单架构的示例,但从未像这样使用复杂架构的示例。
谢谢。
【问题讨论】:
-
您是要更新每本书的all
articleHeadings、本书(或本书的章节)中的所有articleHeadings中的值,还是只更新这一本书,articleHeading带有_id530cc92710f774355434b398? -
做不到。不是我能看到的 - 无论如何都不是在一个查询中。我认为您需要重新考虑架构。 mongodb 的原则之一是为您的访问模式设计架构。
-
有没有关于 mongoose 中良好架构设计的资源(文章)?
标签: javascript node.js mongodb mongoose