如何执行NoSql链接查询 [英] How to do a NoSql linked query

查看:88
本文介绍了如何执行NoSql链接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个noSql(Cloudant)数据库

I have a noSql (Cloudant) database

-在数据库中,我们有一些文档,其中一个文档字段表示表(文档类型)

-Within the database we have documents where one of the document fields represents "table" (type of document)

-在文档中,我们具有表示链接数据库中其他文档的字段

-Within the documents we have fields that represent links other documents within the database

例如:

{_id: 111, table:main, user_id:222, field1:value1, other1_id: 333}

{_id: 222, table:user, first:john, other2_id: 444}

{_id: 333, table:other1, field2:value2}

{_id: 444, table:other2, field3:value3}

我们想要搜索_id:111

We want of way of searching for _id:111

结果是一个包含链接表中数据的文档:

And the result be one document with data from linked tables:

{_id:111, user_id:222, field1:value1, other1_id: 333, first:john, other2_id: 444, field2:value2, field3:value3}

有没有办法做到这一点?

在存储或获取数据的结构上有灵活性b ack-关于如何更好地构造数据以实现这一点的任何建议?

Is there a way to do this?
There is flexibility on the structure of how we store or get the data back—any suggestions on how to better structure the data to make this possible?

推荐答案

首先要说的是Cloudant中没有联接。如果您的架构依赖大量连接,则说明您正在使用Cloudant,这可能意味着您额外的复杂性或性能损失。

The first thing to say is that there are no joins in Cloudant. If you're schema relies on lots of joining then you're working against the grain of Cloudant which may mean extra complication for you or performance hits.

有一种方法在MapReduce视图中取消引用其他文档的ID。它是这样工作的:

There is a way to de-reference other documents' ids in a MapReduce view. Here's how it works:


  • 创建一个MapReduce视图,以 {_id:'linkedid'}

  • 使用 include_docs = true 查询视图以拉回文档和一次取消引用的ID

  • create a MapReduce view to emit the main document's body and its linked document's ids in the form { _id: 'linkedid'}
  • query the view with include_docs=true to pull back the document AND the de-referenced ids in one go

在您的情况下,这样的地图函数:

In your case, a map function like this:

function(doc) {
  if (doc.table === 'main') {
    emit(doc._id, doc);
    if (doc.user_id) {
      emit(doc._id + ':user', { _id: doc.user_id });
    }
  }
}

将允许您后退通过点击 GET / mydatabase / _design / mydesigndoc / _view / myview?startkey = 111& endkey = 111z& include_docs = true 端点:

would allow you to pull back the main document and its linked user document in one API by hitting the GET /mydatabase/_design/mydesigndoc/_view/myview?startkey="111"&endkey="111z"&include_docs=true endpoint:

{
  "total_rows": 2,
  "offset": 0,
  "rows": [
    {
      "id": "111",
      "key": "111",
      "value": {
        "_id": "111",
        "_rev": "1-5791203eaa68b4bd1ce930565c7b008e",
        "table": "main",
        "user_id": "222",
        "field1": "value1",
        "other1_id": "333"
      },
      "doc": {
        "_id": "111",
        "_rev": "1-5791203eaa68b4bd1ce930565c7b008e",
        "table": "main",
        "user_id": "222",
        "field1": "value1",
        "other1_id": "333"
      }
    },
    {
      "id": "111",
      "key": "111:user",
      "value": {
        "_id": "222"
      },
      "doc": {
        "_id": "222",
        "_rev": "1-6a277581235ca01b11dfc0367e1fc8ca",
        "table": "user",
        "first": "john",
        "other2_id": "444"
      }
    }
  ]
}

请注意,我们如何返回两行,第一行是主文档正文,第二行是链接的用户。

Notice how we get two rows back, the first is the main document body, the second the linked user.

这篇关于如何执行NoSql链接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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