Couch DB加入RDBMS之类的操作 [英] Couch DB Join operations like RDBMS

查看:70
本文介绍了Couch DB加入RDBMS之类的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个DB存储普通文档,另一个DB用于存储附件。但我将附件ID存储在普通文档DB中。有没有办法如何执行连接操作两个数据库,如RDBMS。

I am using one DB for storing plain documents and another DB for storing only attachments. But i am storing attachment id in plain documents DB. Is there any way how to perform join operations two databases like RDBMS.

在此先感谢

推荐答案

我知道可以在数据库中进行连接:

I know that it's possible to do joins within a database:

这是一个很棒的帖子: http://www.cmlenz.net/archives/2007/10/couchdb-joins

Here's a great post on that: http://www.cmlenz.net/archives/2007/10/couchdb-joins

它的关键是couchdb可以订购json值,包括列表。

The crux of it is that couchdb can order json values, including lists.

所以让我们从顶部开始......如你所知,CouchDB存储JSON文档。它使用map函数来发出有序键值对(这里是来自couchdb权威指南的页面: http://guide.couchdb.org/draft/views.html )让我们来看一个非常简单的例子,然后是'加入'所需的复杂例子。

So let's take it from the top...CouchDB, as you know, stores JSON documents. It uses map functions to 'emit' ordered key value pairs (here is the page from the couchdb definitive guide: http://guide.couchdb.org/draft/views.html ) So let's take a super simple example, then the complex one we need for 'joins.'

这是一个映射函数,遍历数据库中的每个文档,检查文档是否有作者,如果有,则显示它的作者及其帖子。结果将是按键排序的键值对列表,在本例中为作者(按字母顺序排列)。

This is a map function that goes through each doc in the db, checks if the doc has an author, and if it does, displays it's author and it's post. The result would be a list of 'key value pairs' ordered by keys, in this case authors (and it does so alphabetically).

function (doc) {
  if (doc.author) {
    emit(doc.author, doc.post);
  }
}

现在我们的复杂示例。此示例假定数据库的博客文章为json docs,注释为单独的json文档。评论有一个名为'post'的密钥,其中包含他们所属的博客文章的ID,就像mySQL中的外国ID一样。

Now our complex example. This example assumes that the db has blog posts as json docs, and comments as separate json docs. The comments have a key called 'post' with the id of the blog post they belong to, much like foreign ids in mySQL.

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

那么到底是怎么回事?所以,首先要注意的是其他部分。此函数实际上将博客帖子和评论映射到同一视图(键值对的有序列表)。

So what the heck is going on? So, first thing to notice is the 'else' part. This function actually maps blog posts and comments to the same view (ordered list of key value pairs).

'doc._id'是特定帖子的ID,并且'doc.post'是评论属于两个帖子的ID。所以整个列表都是由博客文章的ID订购的。

'doc._id' is the id of a particular post, and 'doc.post' is the id of the post a comment belongs two. So the whole list is being ordered by the blog post's id.

如果doc.type是'博客文章',它会在数组中附加一个'0'来表示所以,对于那些评论的文档来说是1。现在,这就是魔术。文档(帖子和评论)在一个键值对列表中排序,但由于CouchDB可以对数组进行排序,因此它会在一个博客帖子和下一个博客文章之间滑动那些带有1(评论)的文章。有意义吗?

If the doc.type is a 'blog post' it attaches a '0' in the array to denote so, and a '1' for docs that are 'comments.' Now here's the magic. The docs (posts, and comments) are ordered in one list of key value pairs, but because CouchDB can order arrays, it will slide those with a '1' (comments) in between one blog post and the next. Make sense?

哈哈,与我们习惯的不同,这只是一个创造性的解决方案。还有许多其他方法可以创建您可能需要的结果。希望这有用!

Haha, so different from what we're used to, and this is just one creative solution. There are many other ways to create the result you might need. Hope this is helpful!

这篇关于Couch DB加入RDBMS之类的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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