node.js 更新猫鼬中的嵌套对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23832921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:22:56  来源:igfitidea点击:

Updating nested object in mongoose

node.jsmongodbmongoose

提问by codeofnode

I have searched many questions on nested objects, but all I found where related to array[s].

我搜索了很多关于嵌套对象的问题,但我发现所有与数组 [s] 相关的地方。

I am looking for a updating simple nested object in mongoose.

我正在 mongoose 中寻找一个更新的简单嵌套对象。

From here http://mongoosejs.com/docs/guide.html

从这里http://mongoosejs.com/docs/guide.html

there is an example schema :
var blogSchema = new Schema({
  title:  String,
  author: String,
  body:   String,
  comments: [{ body: String, date: Date }],
  date: { type: Date, default: Date.now },
  hidden: Boolean,
  meta: {
    votes: Number,
    favs:  Number
  }
});

Once created a document,

创建文档后,

How can I change the favs number later on?

以后如何更改收藏号码?

There is no document for the same that I could find.

没有我能找到的相同文件。

This is what I did:

这就是我所做的:

blog.findById(entityId, function(err, mainDoc){
      if(err || !mainDoc) return next(err || 'Document not found');
      var subDoc = mainDoc['meta'];
      if(subDoc){
        subDoc = _.extend(subDoc, { favs : 56 }); //_ lib already available
        console.log(mainDoc.get('meta')); //Prints the updated result with favs = 56  OK
        mainDoc.save(function(err, doc){
           console.log(doc.get('meta')); // prints the updated results with favs = 56 OK
        });
      } else next('Not found');
    });

Everything works file and all console gives the desired result.

一切正常文件和所有控制台都给出了所需的结果。

But when I switch to mongoose console and query the document, I do not get the updated result.

但是当我切换到 mongoose 控制台并查询文档时,我没有得到更新的结果。

I know there can be other ways to achieve the same, but I am only looking for what I am doing wrong in this particular code.

我知道还有其他方法可以实现相同的目标,但我只是在寻找我在此特定代码中做错了什么。

Why the console, after saving document, gives unmatched data from database?

为什么控制台在保存文档后,从数据库中给出了不匹配的数据?

Upon enabling the mongoose debug option, I found the in query there is no such data to be updated. Query fires with blank $set. { $set : {} }

启用 mongoose 调试选项后,我发现查询中没有此类数据要更新。查询以空白 $set 触发。{ $set :{} }

回答by Gergo Erdosi

If you just want to change the value of favs, you can use a simpler query:

如果您只想更改 的值favs,可以使用更简单的查询:

blog.findByIdAndUpdate(entityId, {$set: {'meta.favs': 56}}, function(err, doc) {
    console.log(doc);
});

回答by Brian Noah

The problem is that you can't do anything with data from mongoose once you've got it other than sending it to the client.

问题是,一旦你得到了来自猫鼬的数据,除了将它发送给客户端之外,你不能对它做任何事情。

HOWEVER, there is the lean method that makes it so you can then update the info and do whatever you want with it.

但是,有一种精益方法可以做到这一点,因此您可以更新信息并使用它做任何您想做的事情。

That would look like this:

那看起来像这样:

blog.findById(entityId).lean().exec(function (err, mainDoc) {
    if (err || !mainDoc) {
        return next(err || 'Document not found');
    }
    var subDoc = mainDoc.meta;
    if(subDoc){
        subDoc.favs = 56;
        blog.update({_id: entityId}, mainDoc, function(err, doc){
            console.log(doc.get('meta'));
        });
    } else {
        next('Not found');
    }
});