0

I'm evaluating RethinkDB as a Mongo replacement due to lack of stable Mongo drivers on Node. I've read the docs at http://www.rethinkdb.com/api/javascript/get_all/ and have a very simple question: how do I get a document by key value pair?

The examples for get() only talk about the index. I'm not interested in the index, but rather other fields. Eg, I'd like to be able to do:

.get({someField:SomeValue}) 

... in much the same way I'm used to with collection.find() on Mongo. But that doesn't seem possible. What am I doing wrong? Is fetching a document this way not considered a get?

Should I be using .filter() for this instead?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • 1
    Dunno why you have labelled this as MongoDB, it is about getting stuff from rethinkdb – Sammaye Dec 10 '13 at 16:28
  • 1
    Again I am unsure why you think anyone on the MOngoDB tag would know? – Sammaye Dec 10 '13 at 16:36
  • I don't think the downvote is from them, I think the downvote is that your asking an extremely base question which obviously someone else thinks is so base that you have not bothered to research. One of the many many many downsides to anon voting on SO. The upvotes on my comments existed far before the downvote – Sammaye Dec 11 '13 at 15:41

2 Answers2

4

It looks like Rethink's .filter is the appropriate equivalent to Mongo's find().

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • This is correct. `get` in RethinkDB is reserved for accessing documents using an index. `filter` is used for linear scans which is what you're doing here. – Joe Doliner Dec 10 '13 at 19:28
  • Don't know how I got here in 2018, but `.filter` is to be banned unless chained after a `getAll` or any index-based query that would significantly reduce the selection size, since `.filter` evaluates against _every document_ in the selection. The answer by @Masatoshi is *the* way: create simple, compound or arbitrary indexes, and query on these. – Stock Overflaw Aug 31 '18 at 22:33
  • @StockOverflaw If you understand it, do you want to edit that answer to make it clearer? I don't quite get Masatoshi's English. – mikemaccana Sep 01 '18 at 18:26
  • Errr... I'm not sure I can be clearer that Masatoshi, I mean I don't understand: what troubles you in his post? – Stock Overflaw Sep 01 '18 at 19:11
  • @StockOverflaw add a reference to the banning thing to the other answer if you have one – mikemaccana Sep 01 '18 at 22:24
  • 1
    Just what you needed: this [Medium post](https://medium.com/connect-the-dots/optimizing-queries-in-rethinkdb-584d7f660cb) explains a bit and - more interestingly - shows a benchmark between `.filter` and `.getAll` (spoiler: it's more than 10 times faster in the author's case of a 2-criterium query). Happy reading! ;) – Stock Overflaw Sep 02 '18 at 01:21
  • I just mean .getAll() on indexed table is much faster than .filter(). – Masatoshi Sep 10 '18 at 08:07
  • Because as Joe mentioned, filter() scans linear. In my case, speed of .filter() was not appropriate. So I use .getAll() with index instead of .filter(). – Masatoshi Sep 10 '18 at 08:18
2

You also can use getAll(), but first you create an index for that field:

r.db('test').table('testtable').indexCreate('someField')

You can then use getAll() to get a stream of results:

r.db('test').table('testtable').getAll('SomeValue', {index :'someField'});

This is faster than filter().

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Masatoshi
  • 197
  • 1
  • 6