Skip to content

Instantly share code, notes, and snippets.

View neumino's full-sized avatar

Michel neumino

View GitHub Profile
@neumino
neumino / query.js
Created April 5, 2013 18:49
That works, but it's doesn't look good.
r.expr([
{
"sub_queries" : [
{
"sent_params" : {
"show_take_out_items" : null,
"show_in_store_items" : null,
"show_delivery_items" : null,
"location_id" : 448,
"category_id" : 5333
@neumino
neumino / gist:5448464
Last active December 16, 2015 14:28
Checking if an array of emails contains "guest@guest.com"
r.db("foo").table("bar").filter( lamda doc:
doc["emails"].map(lamda email: email == "guest@guest.com").reduce( lambda acc, val: acc | val, False)
).run(connection)
# Query 1
cursor = r.db('test').table('test').filter(lambda doc:
(doc['status'] == 'open') & doc['baseaddresses'].map(lambda address: address == 'sathis...@example.org').reduce(lambda acc, val: acc | val) ).order_by('date').run()
for doc in cursor:
print doc
# Query 2
cursor = r.db('test').table('test').filter(lambda doc: ((doc['status'] == 'open') | (doc['status'] == 'paused')) & doc['baseaddresses'].map(lambda address: address == 'sathis...@example.org').reduce(lambda acc, val: acc | val) ).order_by('date').run()
{
"values_":{
"1":1,
"2":{
"values_":{
"1":16,
"3":[
{
"values_":{
"1":15,
@neumino
neumino / gist:5607840
Created May 19, 2013 14:36
Update nested object
r.db('test').table('test').update(function(doc) {
return r.branch(
doc('skills').contains('html'),
doc.merge({skills: doc('skills').merge({'html': doc('skills')('html').add(3)})}),
doc.merge({skills: doc('skills').merge({'html': 3})})
)
})
// Suppose needed_category = [1,2,3]
r.db('foo').table('bar')
.filter( function(movie) {
r.expr( needed_category ).map( function(category) {
return category.eq(movie('category');
}).redunce( function(left, right)
return left.or(right);
}, false)
}).run( connection, function(err, cursor) { ... } )
r.table('polls').filter( function(poll) {
return poll("id").eq("d_you_want").and(
poll("processed").map( function(user) { return user.eq("forbidden_user") } ) // map if the user is the one we don't want
.reduce( function(left, right) { return left.or(right) }, false ) // compute a huge OR
.not() // Flip boolean
)
})
reduction := func(left, right r.Exp) r.Exp { return left.Or(right) }
filterFunc := func (job r.Exp) r.Exp {
return job.Attr("categories").Map(func (category r.Exp) r.Exp {
return category.Eq(job.Attr("category")
}).Reduce(reduction, false)
}
r.Table("heroes").Filter(r.Row.Attr("area").Eq("area")).Filter(filterFunc)
@neumino
neumino / gist:5833389
Created June 21, 2013 18:46
For hazardous on IRC
r.table("table_name").filter( function(score) {
return score("scoreboard")("teamPlayerParticipantStats")("externalizedData").map( function(data) {
return data("summonerName")
}).contains("some_string")
.or(
score("scoreboard")("otherTeamPlayerParticipantStats")("externalizedData").map( function(data) {
return data("summonerName")
}).contains("some_string")
)
})
@neumino
neumino / gist:6337823
Created August 26, 2013 03:12
For linuxdisnasty
r.table('apps')
.orderBy("install_date") // order by date
.map( {
name: r.row("name"),
install_date: r.row("name").year().add("-").add(r.row("name").month()).add("-").add(r.row("name").day())
})