Skip to content

Instantly share code, notes, and snippets.

@neumino
Created September 13, 2013 18:11
Show Gist options
  • Save neumino/6554108 to your computer and use it in GitHub Desktop.
Save neumino/6554108 to your computer and use it in GitHub Desktop.
Filtering nested arrays with RethinkDB
r.table("test").filter( function(doc) {
return doc("adresses").contains(function(adress) {
return adress("city").eq("Paris")
})
})
/*
{
name: "Bob",
adresses: [
{
country: "France",
city: "Paris"
}
]
}
*/
@lefam
Copy link

lefam commented Apr 8, 2017

Thanks for this.

@mholt
Copy link

mholt commented Feb 10, 2018

Hey, thanks for posting this. Although, the query does not do what the title says. This query filters documents based on whether a nested array contains a value. It doesn't actually filter non-matching elements out of the array. (Came here via Google search and took me a while to figure out why it wasn't doing what the title said.)

@tudorels
Copy link

tudorels commented Sep 5, 2018

If there are more than three addresses on the fourth the query can not find do not return anything. Do you have any idea if it's a limitation of rethinkdb or I must try to do other way?. Thank you!
Query and data example are bellow:

r.table("test").filter( function(doc) { return doc("adresses").contains(function(adress) { return adress("city").eq("Havana") }) })

{ name: "Bob", adresses: [ { country: "France", city: "Paris" }, { country: "Austria", city: "Vienna" }, { country: "Belgium", city: "Brussels" }, { country: "Cuba", city: "Havana" } ] }

@kedlas
Copy link

kedlas commented Sep 18, 2019

Looks like as required in RethinkDB 2.3.6. It returns the whole objects which contains addresses that contain at least one city=Havana. Of course it returns other addresses too, because only filter for searching was used (no data plucking).

Btw thanks for the query, i was searching for exactly this.

@HeikoMueller
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment