我定义了这个架构:
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更新数据库?我一直在尝试几件事,我似乎无法理解它。
你到处都可以看到简单模式的例子,但从来没有像这样的复杂模式。
THX。
答案 0 :(得分:0)
您始终可以将文档加载到内存中并进行修改,即book[0].bookChapters[0].chapterArticles[0].articleHeadings[0].headingOrder = 100
然后save()
。
上面链接中的示例完全相同,document.find()
后跟save()
。