簡體   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