rql从javascript列表中获取多个文档rethinkdb [英] rql get multple documents from list of keys rethinkdb in javascript

查看:130
本文介绍了rql从javascript列表中获取多个文档rethinkdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个person数据表,它有一个唯一的密钥id。我有一个id列表,我想要将数据作为JSON数组从客户端发送到服务器。该服务将数据接收为JSON数组。

So I have a table of "person" data, which has a unique key "id". I have a list of id's that I want to get the data for which I'll be sending as a JSON array from the client to the server. The serve recieves that data as a JSON array.

现在有一种方法可以运行查询,以获取每个ids的文档?

Now is there a way to run a query that will get the documents for each of those ids?

或者是我唯一的选择,我自己解析ids并构建一个结果数组,然后发送该数组。

Or is my only option to parse the ids myself and build an array of results, then send that array back.

到目前为止我试过使用...

So far I've tried using...


  • getAll - 但我不能让这个工作,因为它接受动态数量的参数,我不知道如何将我的数组值更改为一个动态的参数。

  • getAll - but I cannot get this to work because it accepts a dynamic amount of parameters, and I don't know how to change my array of values into a dynamic amount of parameters.

(示例...我想要做的是显示的内容下面,但我不能)

(example... I want to be able to do whats shown below, but I can't)

r.db('vp').table('user').getAll(["0", "0", "99"])

我只能这样做。

r.db('vp').table('user').getAll("0", "0", "99")


  • expr和forEach - 如下所示,但我不认为这可以工作。

  • expr and forEach - as seen below, but I don't think this can work.

    var arr = [];
    r.expr(["0", "0", "99"]).forEach(function(id) {
    
    })
    


  • 推荐答案

    简答案:

    r.expr([id1, id2, id3]).eqJoin(function(doc) { return doc; }, r.table("person"))
    

    更长的回答:

    是几种方法来做到这一点。以上是我称之为规范的方式。我们分析发生了什么:

    There are a couple of ways to do this. The above is what I'd call the canonical way. Let's breakdown what's happening:

    首先用 r.expr([id1,id2,id3])打包数组将其发送到服务器。

    First with r.expr([id1, id2, id3]) we're packaging up the array to send it over to the server.

    然后我们调用 eqJoin 它做什么是采取价值流和派生和索引获得为每一个。 函数(doc){return doc; } 是一个稍微丑陋的黑客,因为 eqJoin 需要映射函数。

    Then we call eqJoin what it does is take a stream of values and dispatch and indexed get for each one. The function(doc) { return doc; } is a slightly ugly hack because eqJoin requires a mapping function.

    所以在结束上面的代码变成相当于:

    So in the end the above code becomes equivalent to:

    [r.table("person").get(id1), r.table("person").get(id2), r.table("person).get(id3)]
    

    这篇关于rql从javascript列表中获取多个文档rethinkdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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