在CouchDB/Cloudant中创建新文档之前如何检查重复项?

编程入门 行业动态 更新时间:2024-10-11 13:27:11
本文介绍了在CouchDB/Cloudant中创建新文档之前如何检查重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们要检查数据库中是否存在与我们尝试保存的新对象具有相同字段和值的文档,以防止项目重复.

We want to check if a document already exists in the database with the same fields and values of a new object we are trying to save to prevent duplicated item.

注意:此问题不是关于更新文档或关于重复的文档ID,我们只是检查数据以防止使用与现有文档相同的数据来保存新文档.

我们最好通过Mango/Cloudant查询来完成此操作,而不是依赖于视图.

Preferably we'd like to accomplish this with Mango/Cloudant queries and not rely on views.

到目前为止的想法是:

1)扫描我们试图保存的数据,并动态创建一个与该文档的结构匹配的选择器. (我们无法对选择器进行硬编码,因为我们有许多类型的文档)

1) Scan the the data that we are trying to save and dynamically create a selector that matches that document's structure. (We can't have the selectors hardcoded because we have types of many documents)

2)在DB查询数据库中查找与该选择器匹配的所有文档,以查看是否存在符合这些条件的文档.

2) Query de DB with for any documents matching that selector to if any document already exists that matches those criteria.

但是我不知道这种方法的性能,因为许多选择器字段都不会被索引.

However I wonder about the performance of this approach since many of the selector fields will not be indexed.

我还宁愿遵循最佳实践,也不愿突然创建某些东西,但是还没有找到针对该特定场景的任何已知解决方案.

I also much rather follow best practices than create something out of the blue, but haven't been able to find any known solutions for this specific scenario.

如果您碰巧知道任何一个,请分享.

If you happen to know of any, please share.

推荐答案

选项1-为文档定义有意义的ID

该ID可以是逻辑构成,也可以是根据唯一的值计算得出的哈希值

The ID could be a logical coposition or a computed hash from the values that should be unique

如果要检查文档ID是否已存在,可以使用HEAD方法

If you want to check if a document ID already exists you can use the HEAD method

HEAD/db/docId

HEAD /db/docId

如果docId在数据库中退出,则返回200-OK.

which returns 200-OK if the docId exits on the database.

如果要检查新文档中的内容与上一个文档中的内容是否相同,可以使用验证文档更新功能,该功能可以比较两个文档.

If you would like to check if you have the same content in the new document and in the previous one, you may use the Validate Document Update Function which allows to compare both documents.

function(newDoc, oldDoc, userCtx, secObj) { ... }

选项2-使用在CouchDB外部计算的内容哈希

  • 在创建或更新文档之前,应使用应该唯一的属性值来计算哈希值.

  • Before create or update a document a hash should be computed using the values of the attributes that should be unique.

哈希包含在文档中的新属性中,即"key_hash"

The hash is included in the document in a new attribute i.e. "key_hash"

使用"key_hash"属性创建芒果索引

Create a mango index using the "key_hash" attribute

当应插入新文档时,应计算哈希值,并在插入文档之前使用芒果表达式查找具有相同哈希值的文档.

When a new doc should be inserted, the hash should be computed and find for documents with the same hash value using a mango expression before the doc is inserted.

选项3-计算视图中的哈希值

  • 定义一个视图,该视图将每个文档的计算出的哈希值作为键发出

  • Define a view which emit the computed hash for each document as key

  • Couchdb Javascript支持不包括哈希函数,这可能很难包含在设计文档中.
  • 使用 erlang定义地图功能 ,您可以在其中访问散列的erlang支持.
  • Couchdb Javascript support does not include hashing functions, this could be difficult to include in a design document.
  • Use erlang to define the map function, where you can access to the erlang support for hashing.

在创建新文档之前,应使用以前需要计算的哈希值查询视图.

Before creating a new document you should query the view using a the hash that you need to compute previously.

更多推荐

在CouchDB/Cloudant中创建新文档之前如何检查重复项?

本文发布于:2023-10-30 04:39:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:建新   中创   文档   CouchDB   Cloudant

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!