Skip to content

Add a way to find out from ReQL which keys are defined on stored objects #181

Closed

Description

@pixelspark

It is currently difficult to obtain a list of keys defined on objects inside a table. This operation can however be quite helpful for administrative tools. Implementing such an operation is probably relatively easy; I propose adding a 'keys' operation (json.keys() -> json) that returns an array of keys defined on the object. E.g.:

r.expr({x:1, y:2}).keys().run() ==> ['x', 'y']

Using this, it should be possible to enumerate all keys used in a table:

given the following table: [{id:1, x:2, y:3}, {id:2, x:4, y:5}, {id:3, z:6}]

r.db().table().concatMap(function(row) { return row.keys(); }).distinct().run();  ==> ['id','x','y','z']

Activity

  1. pixelspark commented on Dec 24, 2012

    @pixelspark
    Author

    Similarly, it could be useful for admin tools to have a .type() operator that returns the type of a value:

    r.expr({x:1, y:true, s:"string"})("x").type() => integer
    r.expr({x:1, y:true, s:"string"})("y").type() => boolean
    r.expr({x:1, y:true, s:"string"})("s").type() => string
  2. jdoliner commented on Dec 25, 2012

    @jdoliner
    Contributor

    Both good ideas. I think that both of these will be added with the new driver spec which is currently under active development. You can see the actual spec here: https://github.com/rethinkdb/rethinkdb/wiki/protobuf_rfc_raw_spec. Notice that there is a typeof so that one's definitely in there.

    The later I'm not sure about being in the new spec but I know we have talked about our lack of operations on Objects. I think the fundamental operation is just extending map to work on Objects. This would allow you to iterate over them and everything also could just be sugar on top of that. Here's how it would work in my mind:

    > r.expr({x:1, y:2}).map(function (x) { return x; }).run()
    [{key : "x", value : 1}, {key : "y", value : 2}]
    

    This would be the basic operation, then keys could just be sugar for:

    > r.expr({x:1, y:2}).map(function (x) { return x("key"); }).run()
    

    This is another case in which macros would be really nice.

  3. coffeemug commented on Dec 25, 2012

    @coffeemug
    Contributor

    Moving to post-protobuf-improvements.

    Also, to clarify, with the proposed scheme, keys on a table could be defined as follows: table('foo').map(r.row.map(r.row('key'))).distinct()

    EDIT: actually the definition above is wrong because the data passed to distinct would include a stream of arrays (assuming map on an object returns an array, which seems to make sense). So to do this I think we'd need a concatMap here instead of map, like so: table('foo').concatMap(r.row.map(r.row('key')).arrayToStream()).distinct()

  4. mlucy commented on Dec 26, 2012

    @mlucy
    Member

    I agree we should define map on objects. Ruby has this behavior, which I rather like:

    irb(main):003:0> {:a => 1, :b => 2}.map{|x| x}
    => [[:b, 2], [:a, 1]]
    irb(main):004:0> {:a => 1, :b => 2}.map{|k, v| [k, v]}
    => [[:b, 2], [:a, 1]]
  5. coffeemug commented on Jun 10, 2013

    @coffeemug
    Contributor

    Ok, we now support typeOf and as of 1.6 (due this week) we'll also support keys. We'll deal with map over objects separately (there are at least three issues open elsewhere to that affect). Closing.

  6. added 2 commits that reference this issue on Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions