Skip to content

Instantly share code, notes, and snippets.

@brwnll brwnll/gist:7915142
Created Dec 11, 2013

Embed
What would you like to do?
Intent: Convert "day" values to Numbers, to be able to run comparison operators against the row.
// Data sample
[
{
"end": "23:00:00" ,
"day": "5" ,
"id": "003b2c9b-fdae-4112-b29b-6081ae138060" ,
"start": "16:00:00"
},
{
"end": "17:00:00" ,
"day": "6" ,
"id": "00466316-585f-4578-95ab-99431ed2bece" ,
"start": "12:00:00"
},
{
"end": "18:00:00" ,
"day": "0" ,
"id": "067fc39b-2fe8-42d1-a223-533a73998c6b" ,
"start": "12:00:00"
}
]
// Attempted query
r.table("hours")
.filter({"id":"003b2c9b-fdae-4112-b29b-6081ae138060"})
.update(function(doc) { return { day : doc("day") + 1 } })
// Result
{
"end": "23:00:00" ,
"day": "var_21("day")1" ,
"id": "003b2c9b-fdae-4112-b29b-6081ae138060" ,
"start": "16:00:00"
}
@mglukhovsky

This comment has been minimized.

Copy link

commented Dec 11, 2013

@brwnwll, JavaScript doesn't have the luxury of allowing us to overload operators, so to accomplish this in JS, just use add:

// Increment the day
r.table("hours")
  .filter({"id":"003b2c9b-fdae-4112-b29b-6081ae138060"})
  .update(function(doc) { return { day : doc("day").add(1) } })

// Result
{
  "end":  "23:00:00" ,
  "day":  "6" ,
  "id":  "003b2c9b-fdae-4112-b29b-6081ae138060" ,
  "start":  "16:00:00" 
}

In Python and Ruby, you can use +. For more info, check out the docs for add (switch to Python / Ruby to see their operator): http://rethinkdb.com/api/javascript/add/

@mglukhovsky

This comment has been minimized.

Copy link

commented Dec 11, 2013

Note that to get one single document by primary key, you can also use get instead of filter -- of course, filter allows for much more interesting expressions (and finding documents by values other than the primary key).

Also, if you're working with dates and times, you may want to read more about RethinkDB's support for dates: http://rethinkdb.com/docs/dates-and-times/

@brwnll

This comment has been minimized.

Copy link
Owner Author

commented Dec 11, 2013

Thanks, appreciate the help. But when I attempt that query I get an error:

"first_error":  "Expected type STRING but found NUMBER." ,

Using

r.table("hours")
  .filter({"id":"003b2c9b-fdae-4112-b29b-6081ae138060"})
  .update(function(doc) { return { day : doc("day").add(1) } })

Secondarily, I was also looking at the datetime support (pretty awesome), maybe you have some recommendations on queries or data modeling from a ReQL perspective.

What I have is a large number of businesses, with operating hours. (Above is a snippet of the hours table; day (0-6), open/start (24 hr time), close/end (24 hr time).

What I was attempting to glean from the docs: Is there a way for me to make use of ReQL date support when my days are not specific dates, but instead a reference to a day each week. My need will be to do simple, specific time lookups (eg: Which businesses are open Friday at 10pm).

@neumino

This comment has been minimized.

Copy link

commented Dec 11, 2013

You may want to convert all the day fields to number first

r.table("hours").update( {
    day: r.row("day").coerceTo("NUMBER")
})

And then run @mglukhovsky query.

If you want to keep strings for the day field, you can use this query:

r.table("hours")
  .filter({"id":"003b2c9b-fdae-4112-b29b-6081ae138060"})
  .update(function(doc) { 
    return { day : doc("day").coerceTo("NUMBER").add(1).coerceTo("STRING") } 
  })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.