繁体   English   中英

使用 node.js 和 mongoose 更新 Mongo DB 中的数据,即使集合不存在

[英]update data in Mongo DB even when collection does not exist using node.js and mongoose

我需要更新一个存在或不存在的字段:我试过这个

db.foo.update(
    { site: '"wisdom'},
    { $set: {'club': 'fc barcelona'}}, (upsert=true)
)

您可以使用 mongodb 的 upsert:true 选项。基本上,如果查询字符串与文档匹配,它会更新文档,或者如果不匹配,则基本上会创建它。 默认情况下,它的值设置为 false。

db.foo.update(
  { site_id: "xxx" },
  { $set: { "title.de": "", "content.de": "" } },
  {upsert: true}
);

参考: https://docs.mongodb.com/manual/reference/method/db.collection.update/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM