Skip to content

Instantly share code, notes, and snippets.

@banker
banker / sample-mongodb.java
Created January 25, 2011 21:52
A sample program for MongoDB written in Java using the official Java driver.
import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.WriteConcern;
public class Test {
var customerAggregations = provider.AggregationSet<Customer>();
customerAggregations.Apply(x =>x.RecordShell.Age + 3,
"AgeInThreeYears");
public static String script = "" +
"local key = KEYS[1]" +
"local field = ARGV[1]" +
"local value = ARGV[2]" +
"local op = ARGV[3]" +
"local current = redis.call('hget', key, field)" +
"if (current == null) then " +
" redis.call('hset', key, field, value)" +
"elseif op == '>' then" +
" if tonumber(value) > tonumber(current) then" +
@banker
banker / keybase.md
Created November 20, 2018 20:17
keybase.md

Keybase proof

I hereby claim:

  • I am banker on github.
  • I am hwaet (https://keybase.io/hwaet) on keybase.
  • I have a public key ASAz4KeG_T7Aeq6EJ727hEqaMlBSGzGBzx1iwJEONZLoQwo

To claim this, I am signing this object:

@banker
banker / Rails MongoMapper Template.rb
Created October 27, 2009 01:41 — forked from bscofield/gist:181842
A Rails Template for using MongoMapper
# mongo_template.rb
# fork of Ben Scofield's Rails MongoMapper Template (http://gist.github.com/181842)
#
# To use:
# rails project_name -m http://gist.github.com/gists/219223.txt
# remove unneeded defaults
run "rm public/index.html"
run "rm public/images/rails.png"
run "rm public/javascripts/controls.js"
# with multiple collections + commands for renaming back the collections
require 'mongo'
# Stop writing to your database.
# Then, for each collection:
# Specify the DB and COLLECTION you want to convert.
# Will insert all modified documents into a new collection
# called 'new_' + old_collection_name.
# Then you can delete the old collection and rename the new one
@banker
banker / set_benchmark.rb
Created August 30, 2012 16:32
Which of these is the fastest?
x = 10000.times.to_a
a,b,c = x.dup, x.dup, x.dup
Benchmark.measure { 1000.times { out = []; out += a; out += b; out +=c; out.uniq! } }
Benchmark.measure { 1000.times { out = Set.new; out += a; out += b; out +=c; } }
Benchmark.measure { 1000.times { out = {}; a.each {|e| out[e] ||= 1}; b.each {|e| out[e] ||= 1}; c.each {|e| out[e] ||= 1}; } }
> db.f.save({u: 1, v: 2})
> db.f.save({u: 2, v: 2})
> db.f.save({u: 1, v: 5})
> db.f.save({u: 2, v: 10})
> m = function() { emit(this.u, this.v) }
function () {
emit(this.u, this.v);
}
> r = function(k, vs) { r = 0; vs.forEach(function(v) { r += v; }); return r; }
function (k, vs) {
@banker
banker / mongoc
Created January 6, 2012 22:17 — forked from anonymous/mongoc
MongoDB connection via C driver
#include <stdio.h>
#include "mongo.h"
int main() {
mongo conn[1];
int status = mongo_connect( conn, "127.0.0.1", 27017 );
if( status != MONGO_OK ) {
switch ( conn->err ) {
case MONGO_CONN_SUCCESS: printf( "connection succeeded\n" ); break;
@banker
banker / mongodb-at-db-ruby.md
Created September 8, 2011 22:03
MongoDB for Rails at DC Ruby

MongoDB Pointers for Rubyists

Kyle Banker

DC Ruby | September, 2011

THE RAILS

1. Is MongoDB a good choice for your Rails project?