如何通过Mongoose Node.js ORM更新MongoDB文档中的嵌套对象字段



我不知道如何通过Mongoose Node.js JavaScript ORM改变MongoDB文档中嵌套文档中字段的值。CoffeeScript中的代码:

mongoose = require 'mongoose'
mongoose.connect 'mongodb://localhost/test'
Schema = mongoose.Schema
Page = new Schema
  content: String
Article = new Schema
  _id: String
  pages: [Page] 
article_model = mongoose.model 'Article', Article, 'testcollection'
article_model.findOne({_id: 'id1'}, (err, article) =>
  article.pages[0].content = 'foo'
  article.save()
)

下次我取article时,article.pages[0].content仍然是它的原始值,尽管save()没有错误。

我怀疑我需要引用content不同…但如何?谢谢!

编辑:如果我这样做,它会起作用:

for page in article.pages
  if page is whatever
    page.content = 'foo'
article.save()

必须使用update函数

最新更新