简体   繁体   English

猫鼬仅在不存在时插入

[英]Mongoose insert only if doesn't exist

So I don't seem to find anything about just inserting if the specified filter isn't found.因此,如果未找到指定的过滤器,我似乎找不到任何关于仅插入的内容。 I only find stuff on upserting documents but that's not what I want.我只在更新文件上找到东西,但这不是我想要的。 I want to only insert something when that something isn't present in the collection我只想在集合中不存在某些内容时插入某些内容

You should use update() with upsert: true , and then use the $setOnInsert instead of $set .您应该将update()upsert: true使用,然后使用$setOnInsert而不是$set It will insert if the filter does not find a match, otherwise it will do nothing.如果过滤器没有找到匹配项,它将插入,否则它什么都不做。

Collection.update(
   filter,
   { $setOnInsert: update_object },
   { upsert: true }
)

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

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