I have a data schema that looks like this:
{
"datetime": "1453845345493",
"someIds": ["id2000-4", "id1000-34"]
}
Where the array someIds
can contain many different values.
Filtering based on one id using the following query works without issue and returns approximately 400 results. (I also plan to implement proper indexes, but am testing a simpler case for now.)
r.db("dbName").table("tableName")
.filter(r.row("datetime").gt(1453845340493))
.filter(function(someVal){
return someVal("someIds").contains("id2000-4");
})
.count()
But attempting to follow the last example in the 'contains' documentation where it recommends using a predicate function to simulate an 'or' statement returns nothing, when it should return a superset of the above:
r.db("dbName").table("tableName")
.filter(r.row("datetime").gt(1453845340493))
.filter(function(someVal){
return r.expr(["id2000-4", "id1000-77"]).contains(someVal("someIds"));
})
.count()
As far as I can tell I've followed the instructions on that page exactly, but it isn't working. I'm running RethinkDB v2.2.3-1, and am running this in the Data Explorer. Any suggestions?
The example on that page is calling r.expr(ARRAY).contains(SINGLE_VAL)
, while your code is calling r.expr(ARRAY).contains(OTHER_ARRAY)
. If you want to check whether someIds
contains any of the elements in an array, I would use https://www.rethinkdb.com/api/javascript/set_intersection/ for that. (So something like .filter(function(someVal) { return r.expr(ARRAY).setIntersection(someVal('someIds')).count().ne(0); })
.)
If you want to do it with contains
, you could write it like this:
.filter(function(someVal) {
return someVal('someIds').contains(function(someId) {
return r.expr(ARRAY).contains(someId);
});
})