显式设置 CouchDB 文档键

发布于 2024-12-11 22:09:17 字数 759 浏览 0 评论 0原文

CouchDB 文档似乎附有一个密钥;它在检索单个文档时不会显示,但您可以使用它们来检索文档范围,例如:

 wget "http://localhost:5984/monitor20n/_all_docs?startkey=1111&endkey=2222

但是,显然该键始终与文档 ID 相同,因此您获得的只是这样的东西

{"total_rows":14269,"offset":0,"rows":[
{"id":"128127896626798592","key":"128127896626798592","value":{"rev":"1-4e07e2c4b4eddfad5846ddf905337197"}},
{"id":"128128575021907970","key":"128128575021907970","value":{"rev":"1-43d983af1e837a4415b6167cae3b5de8"}},
... and so on }}

(请参阅此处键== ID )。但是,您可以在视图中使用更复杂的键,包括允许更复杂交互的向量;至少,您可以设置视图的键,这样您现在就可以提前搜索内容,而无需查找文档 ID。 现在的问题是:您可以在创建文档时设置这些键吗?或者也许在创建之后? 一个明显的解决方法是创建一个这样的视图

function (doc) {
    emit(doc.key,doc)
}

,但是,我想知道是否有更直接的方法来获得相同的效果。

CouchDB docs seem to have a key attached; it does not show up when retrieving a single document but you can use them to retrieve ranges of documents such as :

 wget "http://localhost:5984/monitor20n/_all_docs?startkey=1111&endkey=2222

However, apparently that key is always the same as the document id, so that all you obtain is stuff like this

{"total_rows":14269,"offset":0,"rows":[
{"id":"128127896626798592","key":"128127896626798592","value":{"rev":"1-4e07e2c4b4eddfad5846ddf905337197"}},
{"id":"128128575021907970","key":"128128575021907970","value":{"rev":"1-43d983af1e837a4415b6167cae3b5de8"}},
... and so on }}

(see here key == id ). However, you can use more complex keys in views, including vectors which allow for much more complex interaction; at least, you can set the keys of views so you can now in advance what to search without looking up document ids.
The question is now: Can you set those keys up when creating a document? Or maybe after creating it?
An obvious workaround is to create a view like this

function (doc) {
    emit(doc.key,doc)
}

however, I would like to know if there's a more direct way of obtaining the same effect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-12-18 22:09:17

键是 CouchDB 视图的重要组成部分。对于视图,键不必是文档 ID。但生成键的唯一方法是在视图中使用 emit 函数。您可以设置的任何属性都不会自动成为键。

将 _all_docs 视为内置视图。为了保持一致,它遵循与常规视图相同的输出,并使用 id 作为键。但您无法更改 _all_docs 视图。如果您想在保存文档时提供自己的_id,那么这将成为关键。
因此,如果您想在“_all_docs”视图中自定义“键”,您可以创建如下文档:

{ _id: 'Sample1' }, {_id: 'My2'}。保存它们后,当您请求“_all_docs”视图时,您将得到:
{“总行数”:2,“偏移量”:0,“行数”:[
{“id”:“样本1”,“键”:“样本1”,“值”:{“rev”:“1-4e07e2c4b4eddfad5846ddf905337197”}},
{"id":"My2","key":"My2","value":{"rev":"1-43d983af1e837a4415b6167cae3b5de8"}},
...等等 }}

这里有一个关于 documentID 的构成的链接:

http://wiki .apache.org/couchdb/HTTP_Document_API#Special_Fields

虽然没有明确说明,但您不能使用对象或数组作为 DocumentID。

希望有帮助。

Keys are an important part of CouchDB views. With a view, the key does not have to be the document ID. But the only way to produce a key is to use the emit function from within a view. There is no property that you can set that will automatically become the key.

Think of _all_docs like a built in view. To be consistent it follows the same output as a regular view, and it uses the id as the key. But you can't change the _all_docs view. If you wanted to provide your own _id when you save a document, that will end up being the key.
So if you wanted custom 'keys' in the '_all_docs' view you could create docs like this:

{ _id: 'Sample1' }, {_id: 'My2'}. and after they are saved, when you request the '_all_docs' view you would get:
{"total_rows":2,"offset":0,"rows":[
{"id":"Sample1","key":"Sample1","value":{"rev":"1-4e07e2c4b4eddfad5846ddf905337197"}},
{"id":"My2","key":"My2","value":{"rev":"1-43d983af1e837a4415b6167cae3b5de8"}},
... and so on }}

Here is a link about what makes a documentID:

http://wiki.apache.org/couchdb/HTTP_Document_API#Special_Fields

While it does not say explicitly, you can't use objects or arrays as DocumentIDs.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文