根据对象属性值在Cloudant(CouchDB)中创建视图

编程入门 行业动态 更新时间:2024-10-11 17:21:38
本文介绍了根据对象属性值在Cloudant(CouchDB)中创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在试图找到解决此问题的方法,但是我遇到了很多死胡同。

I've been trying to find a solution for this requirement but I've hit many dead ends.

我正在使用 Cloudant 作为我的用户文档数据存储。每个用户文档都有一个称为项目的字段(属性),该字段是一个对象数组。

I'm using Cloudant as my data store of user documents. Each user document has a field (property) called 'items', which is an array of objects.

因此,用户文档如下所示:

So a user document looks like this:

{ "_id":"userid1", "_rev":"XX", "username": "foobaruser" "items": [ { "date":1357879144069, "text":"don't cry because it's over, smile because it happened.", "cat":"determination" }, { "date":1357879179209, "text":"those who mind don't matter, and those who matter don't mind.", "cat":"fitness" }, { "date":1357883809736, "text":"be the change that you wish to see in the world.", "cat":"determination" }, { "date":1357879179209, "text":"those who mind don't matter, and those who matter don't mind.", "cat":"hardwork" }, { "date":1357879179209, "text":"those who mind don't matter, and those who matter don't mind.", "cat":"determination" } ] }

  • 数据存储中有多个这样的用户文档,每个文档都具有项目属性
  • 要求:

  • 理想情况下,我想在视图上使用搜索功能并通过

  • Ideally, i'd like to use a search function on a view and pass in a value for "cat", which then returns all the "items" in all docs that match the value of "cat".

    例如,在 cat的值中返回所有 item中所有与 cat的值匹配的 items。 https:// [用户名] .cloudant / dbname / _design / views / _search / doc?q =确定

    e.g. [username].cloudant/dbname/_design/views/_search/doc?q=determination

    以上搜索将以类似于以下格式返回所有用户文档中所有具有 cat =确定的项目中的对象:

    The above search will return all the objects in "items" across all user docs which have "cat = determination" in a format similar to this:

    { "total_rows": 2, "rows": [{ "id": "userid1", "items": [ { "date":1357879144069, "text":"don't cry because it's over, smile because it happened.", "cat":"determination" }, { "date":1357883809736, "text":"be the change that you wish to see in the world.", "cat":"determination" }, { "date":1357879179209, "text":"those who mind don't matter, and those who matter don't mind.", "cat":"determination" } ] }, { "id": "userid2", "items": [ { "date":135787966655, "text":"Some text", "cat":"determination" } ] }] }

  • 如果使用搜索无法做到这一点,那么有可能使用二级索引来实现这一目标吗?

  • If this is not possible using "search", then is it possible to use secondary-indexes to achieve this?

    推荐答案

    在CouchDB中,您不能为视图的查询参数(在您的情况下为cat = determining)。 CouchDB中的方法是创建一个更通用的视图,然后在调用该视图以获取所需数据时调整结果的排序方式。

    In CouchDB you can't pass in a dynamic value for a query parameter to a view (in your case cat=determination). The approach in CouchDB is to create a more general view, and then adjust how the result is sorted when you call the view to get at the data you need.

    ll需要在数据库的设计文档中创建自定义视图以实现以下目的:

    You'll need to create custom view in your db's design document to achieve this:

    byUserItemCat: { map: function (doc) { if ( !doc.items || doc.items.length === 0 ) return; for ( var i=0; i<doc.items.length; i++ ) { emit(doc.items[i].cat,{doc._id,doc.items[i].date,doc.items[i].text}); } } }

    数据库中的doc,检查它是否具有包含内容的items数组,然后遍历所有doc的项目。对于发现的每个item元素,它都会向结果集中发出 cat 作为索引,这很重要,因为我们可以根据该索引进行排序。我们可以按照自己喜欢的方式随意构建结果对象( emit 的第二个参数),在上述情况下,我将使用用户ID构建对象,并且该项目的日期和文本。

    So the above view takes each doc in the db, checks it has an items array with contents, and then loops over all the doc's items. For each item element it finds it emits cat into the result set as the index, this is important since we can then sort against against this index. We're free to build the result object in anyway we like (second argument to emit), and in the above case I'm building an object with the user id, and the item's date and text.

    您将调用这样的视图以获取所有结果:

    You'd call the view something like this, to get all the results:

    curl -X GET 127.0.0.1:5984/<db-name>/_design/<design-doc-name>/_view/byUserItemCat

    如果您只是对索引键(例如 cat )是您要做的确定:

    And if you were just interested in the results where the index key (i.e. cat) was "determination" you'd do:

    curl -X GET 127.0.0.1:5984/<db-name>/_design/<design-doc-name>/_view/byUserItemCat?key="determination"

更多推荐

根据对象属性值在Cloudant(CouchDB)中创建视图

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

发布评论

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

>www.elefans.com

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