Skip to content

Instantly share code, notes, and snippets.

View neumino's full-sized avatar

Michel neumino

View GitHub Profile
r.table('posts').indexStatus('oldIndex').​nth(0).do(function(oldIndex) {
return r.table('posts').indexCreate('newIndex', oldIndex("function"), {multi: oldIndex("multi")}).do(function() {
return r.table('posts').indexWait('newIndex').do(function() {
return r.table('posts').indexRename('newIndex', 'oldIndex', {overwrite: true})
})
})
})
r.expr([{ v: 1 }, { v: 2 }, { v: 3 }, { v: 4 }]).map(function(doc) {
return [doc('v')]
}).reduce(function(left, right) {
return left.union(right).orderBy(function(element) { return element.mul(-1)}).limit(2)
}).nth(1)
Possibly unhandled TypeError: Cannot call method 'call' of undefined
at tryCatch1 (/home/michel/projects/rethinkdb-all/thinky/node_modules/bluebird/js/main/util.js:43:21)
at CatchFilter$_doFilter [as doFilter] (/home/michel/projects/rethinkdb-all/thinky/node_modules/bluebird/js/main/catch_filter.js:80:27)
at tryCatch1 (/home/michel/projects/rethinkdb-all/thinky/node_modules/bluebird/js/main/util.js:43:21)
at Promise$_callHandler [as _callHandler] (/home/michel/projects/rethinkdb-all/thinky/node_modules/bluebird/js/main/promise.js:627:13)
at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/home/michel/projects/rethinkdb-all/thinky/node_modules/bluebird/js/main/promise.js:641:18)
at Promise$_settlePromiseAt [as _settlePromiseAt] (/home/michel/projects/rethinkdb-all/thinky/node_modules/bluebird/js/main/promise.js:800:14)
at Promise$_settlePromises [as _settlePromises] (/home/michel/projects/rethinkdb-all/thinky/node_modules/bluebird/js/main/promise.js:934:14)
at
// Index:
r.table("heroes").indexCreate("idEquipment", function(doc) {
return doc("equipment").map(function(equipment) {
return [doc("id"), equipment]
})
}, {multi: true}).run(conn, callback)
// Query
r.table("heroes").getAll([1, "boots"], {index: "idEquipment"}).run(conn, callback)
@neumino
neumino / gist:455aa5b1cb8d8a732f5c
Created June 18, 2014 21:33
Pivot example with r.object
r.db('test').table('marks')
.group('name')
.concatMap(function(row){return [row('course'), row('mark')];})
.ungroup()
.map(function(res){
return r.expr({name: res('group')})
.merge(r.object(r.args(res('reduction'))));
})
@neumino
neumino / gist:8920871
Created February 10, 2014 17:55
10/02/2014 - @miguelping - Twitter
r.table("foo").groupedMapReduce(
function(doc) { return doc("categorie") },
function(doc) { return doc.pluck("a", "b") },
function(left, right) {
return {
a: left("a").add(right("a")),
b: left("b").add(right("b"))
}
}
).run(...)
@neumino
neumino / gist:8789766
Created February 3, 2014 18:44
IRC - richcorbs - 2014/02/03
r.table('stuff').filter({'this_field' => 'this value'}).grouped_map_reduce(
lambda {|doc| doc[:this_other_field]} },
lambda {|doc| [doc] },
lambda {|left, right| left+right}
).run(conn)
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")
)
})
@neumino
neumino / gist:8363888
Created January 10, 2014 22:24
jakcharlton - IRC - 2014.01.10
// 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}),
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