在CouchDB/Cloudant中创建新文档之前如何检查重复项? [英] How to check for duplication before creating a new document in CouchDB/Cloudant?

查看:121
本文介绍了在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 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中创建新文档之前如何检查重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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