Skip to content

Instantly share code, notes, and snippets.

View ggoodale's full-sized avatar

Grant Goodale ggoodale

View GitHub Profile
@ggoodale
ggoodale / gist:300869
Created February 10, 2010 21:30
Explain() on a query in mongodb
> db.users.find({last_logged_in : {'$ne': null}}).explain()
{
"cursor" : "BasicCursor",
"startKey" : {
},
"endKey" : {
},
"nscanned" : 536299,
@ggoodale
ggoodale / gist:300870
Created February 10, 2010 21:32
Same query, with a hint
> db.users.find({last_logged_in : {'$ne': null}}).hint({last_logged_in: 1}).explain()
{
"cursor" : "BtreeCursor last_logged_in_1",
"startKey" : {
"last_logged_in" : {
"$minElement" : 1
}
},
"endKey" : {
"last_logged_in" : {
@ggoodale
ggoodale / gist:300874
Created February 10, 2010 21:33
Using $gt instead of $ne
> db.users.find({last_logged_in : {'$gt': null}}).explain()
{
"cursor" : "BtreeCursor last_logged_in_1",
"startKey" : {
"last_logged_in" : null
},
"endKey" : {
"last_logged_in" : {
"$maxElement" : 1
}
@ggoodale
ggoodale / gist:300880
Created February 10, 2010 21:40
Same query using $gt: 0
> db.users.find({last_logged_in : {'$gt': 0}}).explain()
{
"cursor" : "BtreeCursor last_logged_in_1",
"startKey" : {
"last_logged_in" : 0
},
"endKey" : {
"last_logged_in" : 1.7976931348623157e+308
},
"nscanned" : 0,
@ggoodale
ggoodale / gist:300896
Created February 10, 2010 21:56
Same query with a date obj in the $gt clause.
> db.users.find({last_logged_in : {'$gt': new Date(0)}}).explain()
{
"cursor" : "BtreeCursor last_logged_in_1",
"startKey" : {
"last_logged_in" : "Wed Dec 31 1969 16:00:00 GMT-0800 (PST)"
},
"endKey" : {
"last_logged_in" : "Tue Jan -2147483647 584556020 06:25:52 GMT-0800 (PST)"
},
"nscanned" : 8,
@collection = db.collection('docs')
loop do
# Find a record to work on
new_record = @collection.find_one({:state => "NEW"}, {:sort => "created_at asc"})
# Set the state to my process id, but only if it's still in the "NEW" state
response = @collection.update({:_id => new_record.id, :state => "NEW"},
{'$set' => {:state => Process.pid}},
{:upsert => false, :safe => true})
db_config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))
if db_config[Rails.env]
mongo = db_config[Rails.env]
mongo_host = mongo['host'] || 'localhost'
mongo_port = mongo['port'] || 27017
RAILS_DEFAULT_LOGGER.error("Connecting to #{mongo_host}:#{mongo_port}")
MongoMapper.connection = Mongo::Connection.new(mongo_host, mongo_port, :logger => RAILS_DEFAULT_LOGGER)
MongoMapper.database = File.basename(mongo['database']).split(".")[0]
end
> db.tiles.find({ position: { $within: { $box: [ [ 1466, -2128 ], [ 1506, -2088 ] ] } } }).explain()
{
"cursor" : "GeoBrowse-box",
"nscanned" : 341,
"nscannedObjects" : 341,
"n" : 341,
"millis" : 657,
"indexBounds" : {
}
> db.tiles.find({ position: { $within: { $box: [ [ 1466, -2128 ], [ 1506, -2088 ] ] } } }).explain()
{
"cursor" : "GeoBrowse-box",
"nscanned" : 341,
"nscannedObjects" : 341,
"n" : 341,
"millis" : 725,
"indexBounds" : {
}
@ggoodale
ggoodale / SingingChat.js
Created November 8, 2010 01:08
Node.js example #2 From Deploy 2010
var net = require('net'), fs = require('fs'), _ = require('./underscore-min')._;
// Our active connections
var connections = {};
var doc = fs.readFileSync('lyrics.txt', 'utf8').split(" ");
var word = 0;
var intervalId = null;
var sing = function() {
if ((word >= doc.length || _.size(connections) == 0) && intervalId != null) {