javascript - 即使集合不存在使用 node.js 和 mongoose 更新 Mongo DB 中的数据
问题描述
我需要更新一个存在与否的字段:我试过这个
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/
推荐阅读
- tsql - 需要 T-SQL 游标的帮助,本质上是检索上一行 +1 的整数
- javascript - 如何使用循环和 if else 语句分配随机数?
- python - 将列和默认数据附加到新的 Pandas DataFrame 中
- amazon-web-services - 我可以使用 Cloudformation 在特定可用区中创建子网吗?
- vba - 在 VBA 中使用列函数中的变量作为参数
- sql - 如何根据日期以一个月的增量填充表格
- javascript - 文件未加载到 HTML 文本区域
- php - 如何使用php将单个文件拆分为多个文件?
- python - 在提交之前如何检索对模型实例所做的更改?
- php - 未捕获的异常 'Exception' 带有消息 'Option `access_token` 只能是一个字符串。