最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dynamic - rql get multiple documents from list of keys rethinkdb in javascript - Stack Overflow

programmeradmin0浏览0评论

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.

Now is there a way to run a query that will get the documents for each of those 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 - 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"])
    

    I can only do this...

    r.db('vp').table('user').getAll("0", "0", "99")
    
  • expr and forEach - as seen below, but I don't think this can work.

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

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.

Now is there a way to run a query that will get the documents for each of those 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 - 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"])
    

    I can only do this...

    r.db('vp').table('user').getAll("0", "0", "99")
    
  • expr and forEach - as seen below, but I don't think this can work.

    var arr = [];
    r.expr(["0", "0", "99"]).forEach(function(id) {
    
    })
    
Share Improve this question edited Dec 5, 2018 at 3:15 Cœur 38.7k26 gold badges202 silver badges277 bronze badges asked Dec 3, 2013 at 10:00 CamHartCamHart 4,3357 gold badges36 silver badges75 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 19

You can also use getAll with a dynamic array. You have to resort to apply

var ids = [1,2,3];
var table = r.table("person");
table.getAll.apply(table, ids).run( connection, callback);

Note that now since 1.12, you can just do

var ids = [1,2,3];
r.table("person").getAll(r.args(ids)).run(connection, callback)

short answer:

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

Longer answer:

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:

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

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)]

Update for 2017. Use the ES6 spread operator '...'

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

You can use .expr with .map for a cleaner alternative to .eqJoin

r.expr(array).map(function(id) {
  return r.table('user').get(id);
});
发布评论

评论列表(0)

  1. 暂无评论