2011-08-02 71 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() 

然而,这似乎是相当不雅,效率低下。

回答

相关问题