将N1QL与文档密钥一起使用 [英] Using N1QL with document keys

查看:52
本文介绍了将N1QL与文档密钥一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Couchbase还是很陌生,并且一直试图找到我要创建的特定查询的答案,但到目前为止并没有成功.

I'm fairly new to couchbase and have tried to find the answer to a particular query I'm trying to create with not much success so far.

我一直在为这种情况使用视图或N1QL与N1QL达成和解,但一直没有设法使它起作用,所以也许毕竟视图更好.

I've debated between using a view or N1QL for this particular case and settled with N1QL but haven't managed to get it to work so maybe a view is better after all.

基本上,我具有以下文档的文档密钥(Group_1):

Basically I have the document key (Group_1) for the following document:

Group_1
{
  "cbType": "group",
  "ID": 1,
  "Name": "Group Atlas 3",
  "StoreList": [
    2,
    4,
    6
  ]
}

我也有存储"文档,其密钥列在此文档的存储列表中.(Store_2,Store_4,Store_6,它们的storeID值分别为2、4和6),我基本上想获取列出的所有3个文档.

I also have 'store' documents, their keys are listed in this document's storelist. (Store_2, Store_4, Store_6 and they have a storeID value that is 2, 4 and 6) I basically want to obtain all 3 documents listed.

我真正起作用的是,我通过执行以下操作获得了具有其ID的文档:

What I do have that works is I obtain this document with its id by doing:

var result = CouchbaseManager.Bucket.Get<dynamic>(couchbaseKey);
mygroup = JsonConvert.DeserializeObject<Group> (result.ToString());

然后,我可以遍历它的存储列表并以相同的方式获取它的所有存储,但是我不需要该组中的任何其他信息,我想要的只是存储,因此我宁愿在单个操作中执行此操作.

I can then loop through it's storelist and obtain all it's stores in the same manner, but i don't need anything else from the group, all i want are the stores and would have prefered to do this in a single operation.

有人知道如何直接对指定的文档值执行N1QL吗?诸如此类(这是完全虚构的非工作代码,我只是想清楚地说明我要达到的目的):

Does anyone know how to do a N1QL directly unto a specified document value? Something like (and this is total imaginary non working code I'm just trying to clearly illustrate what I'm trying to get at):

选择*从mycouchbase中的文档密钥输入 Group_1.StoreList

谢谢

更新:因此Nic的解决方案不起作用;

UPDATE: So Nic's solution does not work;

这是我最需要的自动取款机:

This is the closest I get to what I need atm:

SELECT b from DataBoard c USE KEYS ["Group_X"] UNNEST c.StoreList b;

"results":[{"b":2},{"b":4},{"b":6}]

这将返回我想要的任何给定组(Group_X)的商店ID的列表-我还没有找到一种方法来获取完整的商店,而不仅仅是同一条语句中的ID.

Which returns the list of IDs of the Stores I want for any given group (Group_X) - I haven't found a way to get the full Stores instead of just the ID in the same statement yet.

一旦有了,我将发布完整的解决方案以及在此过程中遇到的所有减速带.

Once I have, I'll post the full solution as well as all the speed bumps I've encountered in the process.

推荐答案

如果您对您的问题有误解,我深表歉意,但我会尽力而为.如果我误会了,请告诉我,我们将从那里开始.

I apologize if I have a misunderstanding of your question, but I'm going to give it my best shot. If I misunderstood, please let me know and we'll work from there.

让我们使用以下情形:

group_1

{
    "cbType": "group",
    "ID": 1,
    "Name": "Group Atlas 3",
    "StoreList": [
        2,
        4,
        6
    ]
}

商店_2

{
    "cbType": "store",
    "ID": 2,
    "name": "some store name"
}

store_4

{
    "cbType": "store",
    "ID": 4,
    "name": "another store name"
}

store_6

{
    "cbType": "store",
    "ID": 6,
    "name": "last store name"
}

现在可以说您不想查询特定组(group_1)中的商店,但是不包括有关该组的其他信息.您本质上想使用N1QL的 UNNEST JOIN 运算符.

Now lets say you wan't to query the stores from a particular group (group_1), but include no other information about the group. You essentially want to use N1QL's UNNEST and JOIN operators.

这可能会给您一个类似这样的查询:

This might leave you with a query like so:

SELECT 
    stores.name 
FROM `bucket-name-here` AS groups 
UNNEST groups.StoreList AS groupstore
JOIN `bucket-name-here` AS stores ON KEYS ("store_" || groupstore.ID) 
WHERE 
    META(groups).id = 'group_1';

在此做出一些假设.您的两个文档都位于同一存储桶中,并且您只想从 group_1 中进行选择.当然,您可以使用 Like 并将组ID切换为百分号通配符.

A few assumptions are made in this. Both your documents exist in the same bucket and you only want to select from group_1. Of course you could use a LIKE and switch the group id to a percent wildcard.

让我知道某些事情是否有意义

Let me know if something doesn't make sense.

最好

这篇关于将N1QL与文档密钥一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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