2011-08-02 75 views
0

我無法弄清楚如何通過Mongoose Node.js JavaScript ORM更改MongoDB文檔中嵌套文檔中字段的值。代碼的CoffeeScript:如何通過Mongoose Node.js ORM更新MongoDB文檔中的嵌套對象字段?

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() 
) 

下一次我取articlearticle.pages[0].content仍然有其原始值,雖然對save()沒有錯誤。

我懷疑我需要引用content的區別......但是如何?謝謝!

編輯:它的工作原理,如果我做這樣的事情:

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

然而,這似乎是相當不雅,效率低下。

回答

相關問題