Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RethinkDB: iterating over object properties

Tags:

rethinkdb

I have the following data structure:

wall

{
    slug: "wall-slug",
    nodes: {
        "node1": "id-from-nodes-table-1",
        "node2": "id-from-nodes-table-2"
    }
}

nodes

{
    id: "id-from-nodes-table-1",
    something: "something"
}

Trying to merge document from nodes table into definite node in nodes object in wall table in this way:

r.db("test").table("walls").getAll("wall-slug", {index: "slug"}).map(function(row) {
    return row.merge({nodes: row("nodes").map(function(node) {
        return r.db("test").table("nodes").get(node);
    })});
})

And it supposed to look like this:

{
    slug: "wall-slug",
    nodes: {
        "node1": {object from nodes table got by value from this property},
        "node2": {object from nodes table got by value from this property}
    }
}

But I get "Cannot convert OBJECT to SEQUENCE" message - couldn't find a way to iterate over nodes object properties and replace it's property values with objects from another table - is there any?

like image 960
La Faulx Avatar asked Feb 18 '14 19:02

La Faulx


1 Answers

map iterates on an array or stream, not an object. You can use keys() to get the keys, then iterate on them.

Here's what the query looks like:

r.db("test").table("walls").getAll("wall-slug", {index: "slug"}).map(function(row) {
  return row.merge({nodes: 
    row("nodes").keys().map(function(key) {
      return r.expr([key, r.db("test").table("nodes").get(row("nodes")(key))])
    }).coerceTo("object")
  })
})
like image 132
neumino Avatar answered Nov 18 '22 15:11

neumino