CouchDB view sorting, combine by one key, search by other values

Looking at the example described in Adds a Douch Douch .

It discusses mapping views and how you can have one document for your blog posts, and then each comment is a separate document in CouchDB. So, for example, I could have “My post” and 5 comments related to “My post”, a total of 6 documents. In their example, “myslug” is stored both in the mail document and in each comment document, so when I search for CouchDB with the key “myslug”, it returns all documents.

Here is the problem / question. Let's say I want to search for the author in the comments and publication, which also has the category of "news." How will it work?

So for example:

function(doc) { if (doc.type == "post") { emit([doc._id, 0], doc); } else if (doc.type == "comment") { emit([doc.post, 1], doc); } } 

This will load my blog post and comments based on this ?startkey=["myslug"]

However, I want to do this, grab the author’s comments from bob and a post that has category news. In this example, bob wrote three comments on a category news blog post. It seems that CouchDB allows me to search only the keys that exist in both documents, and not search by the key in one document and the key in another, which is “connected” with the card function.

In other words, if the message and comments are combined in slug, how can I search in one field in one document and in another field in another document, to which the identifier aka joins. bullets?

In SQL, it will be something like this:

 SELECT * FROM comments JOIN doc.id ON doc.post WHERE author = bob AND category = news 
+4
source share
2 answers

I’ve been studying couchdb for about a week, so I’m hardly ready to answer your question, but I think I have come to the conclusion that this is not possible. You need to bind the results to one and only one document so that the presentation can be updated. You will have to denormalize, at least if you don't want to do a grunt search. If anyone comes up with a smart way to do this, I really would like to know.

0
source

There are several ways to approximate an SQL connection on CouchDB. I just asked a similar question here: Why is CouchDB's reduce_limit enabled by default? (Is it better to approximate SQL JOINS in MapReduce views or in list views?)

  • You can use MapReduce (not a good option).
  • You can use lists (this will lead to a jump in the set of results before the results are displayed, which means that you can "combine" documents in several ways).
  • You can also use “matching”, although I have not figured it out yet (it seems that I always get an account and can use this function only with the help of “Decrease”), if I'm on the right track)
0
source

Source: https://habr.com/ru/post/1339935/


All Articles