如何使用CouchBase进行原子“如果未找到则创建”

时间:2021-07-08 19:40:32

For example I want to check if the user input email are already exist or not, then if only not found, save that user object to CouchBase.
Can I do that with N1QL?
Can I do that with this native driver: https://github.com/couchbaselabs/gocb
Thanks

例如,我想检查用户输入的电子邮件是否已经存在,如果只是找不到,则将该用户对象保存到CouchBase。我可以用N1QL做到吗?我可以使用这个本机驱动程序执行此操作:https://github.com/couchbaselabs/gocb谢谢

1 个解决方案

#1


3  

The simplest way would be if the email address is the objectID or part of the object ID. If it is, you just do an insert() with the objectID and you're done.

最简单的方法是,如果电子邮件地址是objectID或对象ID的一部分。如果是,你只需使用objectID执行insert()就可以了。

If you cannot use the email in the ObjectID, then you will have to do a view or use N1QL. The first one using the ObjectId will of course be the most performant. Key lookups always are.

如果您无法在ObjectID中使用该电子邮件,则必须执行查看或使用N1QL。使用ObjectId的第一个当然是最高性能的。密钥查找总是如此。

#1


3  

The simplest way would be if the email address is the objectID or part of the object ID. If it is, you just do an insert() with the objectID and you're done.

最简单的方法是,如果电子邮件地址是objectID或对象ID的一部分。如果是,你只需使用objectID执行insert()就可以了。

If you cannot use the email in the ObjectID, then you will have to do a view or use N1QL. The first one using the ObjectId will of course be the most performant. Key lookups always are.

如果您无法在ObjectID中使用该电子邮件,则必须执行查看或使用N1QL。使用ObjectId的第一个当然是最高性能的。密钥查找总是如此。