猫鼬更新对象,不带"drop"标志 [英] mongoose Update object without "drop"

查看:61
本文介绍了猫鼬更新对象,不带"drop"标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的猫鼬和安培有问题更新文档.

i have there a problem with mongoose & update documents.

当我更新集合中的对象时,它会清除/删除"该对象,并用更新的对象填充它.但是我需要添加/合并"对象.

when i upate a object in a collection then it "clear/drop" the object, and fill it with the to update object. But i need to "add/merge" the objects.

例如:

Model.update({name: "hello"}, {
  name: "hello",
  datum: {
    updated: Date.now(),
    //added: Date.now()
  }
}, function(err, data){
  
  console.log(err, data);
  
});

这将我的基准"对象替换为已更新"字段. 添加"字段被删除.为什么 ? 如何更新嵌套的基准"对象?

This replace my "datum" object with on field: "updated. The "added" filed is deleted. WHY ? How can i update the nested "datum" object ?

推荐答案

您需要使用$set运算符.如果未指定,则与查询文档(第一个参数)匹配的第一个文档将由更新文档(第二个参数)替换:

You need to use the $set operator. If you don't specify it, the first document that matches your query document (1st parameter) will get replaced by your update document (2nd parameter) :

Model.update({name: "hello"}, {
  $set: {
      "datum.updated": Date.now()
  }
}, function(err, data){
    console.log(err, data);
});

这篇关于猫鼬更新对象,不带"drop"标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆