插入 + 更新/向上插入? [英] Insert + Update / Upsert ?

查看:26
本文介绍了插入 + 更新/向上插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题:在 mongo(或任何与此相关的数据库)中,upsert 查询是否是插入 + 更新查询的更好替代方法.

Quick question: In mongo ( or any db for that matter), are upsert queries a better alternative to insert + update query.

upsert -> 如果找到并更新文档,则插入一个新文档.我读过文章说这让生活更轻松并且性能有效.(本质上是两个操作)insert -> 检查文档是否存在,如果不存在 -> 插入文档否则抛出错误.( 2 次操作)update 将与插入相反.(再次操作 2 次)

upsert -> finds and updates document if found else inserts a new document. I've read articles saying this makes life easier and is performance effective. (Essentially two operations) insert -> checks if document exists, if not -> insert document else throw an error. ( 2 operations) update would be the reverse of insert. (2 operations again )

从这个 upsert 似乎是一个更好的选择,但如果我有很少更新的数据怎么办.会有一个 insert &update 查询更有意义还是 upsert 操作?

From this upsert seems to be a better choice but what if I had data which rarely gets updated. Would having an insert & update query make more sense or an upsert operation?

推荐答案

如果您主要是插入新数据,但偶尔在这些数据中可能有您知道要更新的文档,那么 upsert 将节省您编写额外的错误处理或检查例程.在这种情况下,它会更好".不过,它确实取决于您的数据以及您希望如何处理异常.确保您可以在没有警告的情况下更新文档,并且不会因现有数据而导致插入失败.

If you're mostly inserting new data but occasionally amongst that data there could be a document that you know you will want to update, then upsert will save you having to code extra error handling or checking routines. In that case it would be "better". It does depend on your data and how you want to handle exceptions, though. Make sure you are OK with updating documents with no warning, and that there are no cases where you want an insert to fail due to existing data.

这篇关于插入 + 更新/向上插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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