This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.table("test").filter( function(doc) { | |
return doc("adresses").contains(function(adress) { | |
return adress("city").eq("Paris") | |
}) | |
}) | |
/* | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.table("test").get("idValue").update(function(doc) { | |
return r.branch( | |
doc("lastUpdate").eq(newDoc["lastUpdate"]), | |
newDoc.merge({lastUpdate: r.now()}), | |
r.error("Wrong version") | |
) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Retrieve all the documents where val == 1 | |
// If no document is returned, insert {newVal: 2} | |
// Else update all the documents with {newVal: 2} | |
// | |
// Note: This query is not atomic | |
r.table("test").filter({val: 1}).count().do(function(numResults) { | |
return r.branch( | |
numResults.eq(0), | |
r.table("test").insert({newVal: 2}), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rdbConn = {}; | |
var r = require('rethinkdb'); | |
if (r.protobuf_implementation !== 'cpp') console.log("WARNING: Wrong protobuf backend"); | |
var async = require('async'); | |
// Parameters | |
var numInserts = 5000; | |
// Global var |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.table("t1").eqJoin( | |
function(doc) { return doc("a")("b"); }, | |
r.table("t2") | |
).map( function(left, right) { | |
return left.merge({ | |
a: { | |
b: right | |
} | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.db('shred').table("teams").map(function(team) { | |
return team.merge({ | |
members: r.db('shred').table("users").filter(function(user) { | |
return team("members").contains(user("id")) | |
}).coerceTo("ARRAY") | |
}) | |
}) | |
// More efficient query that uses the index id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
conn = r.connect() | |
cursor = r.db('gbrl').table('race').get_all(14136, index='season_id') \ | |
.pluck({'results':['driver', 'position']}, 'time') \ | |
.order_by('time') \ | |
.map( lambda race: | |
r.branch( # That's just a if | |
# Check if Andrew is in the results | |
race["results"]["driver"].contains("Andrew Vennaro"), | |
race["results"].filter(lambda doc: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.table("users").filter( function(user) { | |
// map over all the keys of search | |
return r.expr(search).keys().map( function(key) { | |
// for each key, we return a boolean weather user(key) match search(key) (case insensitive) | |
return user(key).match( r.expr("(?i)").add(r.expr(search)(key)) ).ne(null) | |
}).reduce( function(left, right) { // Then for each boolean, we return left and right // so we perform a "and" operation here. | |
return left.and(right) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.db('todomvc').table('posts').map( function(post) { | |
return post.merge({ | |
comments: r.db('todomvc').table('comments').filter( function(comment) { | |
comment("pid").eq( post("id") ) | |
}).coerceTo("ARRAY") | |
}) | |
}) | |
// If you have an index on pid for the table comments, you can do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.db("ScrobbleGraph").table("scrobbles").groupedMapReduce( | |
function(doc) { return doc("date").date() }, | |
function(doc) { return [doc] }, | |
function(left, right) { return left.add(right)} | |
) | |
// It's better to use just an orderBy and group on the client because | |
// - The database won't have to keep everything in memory | |
// - It's faster because of the index |
NewerOlder