This file contains hidden or 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
# Items and Categories in MongoMapper | |
require 'rubygems' | |
require 'mongo_mapper' | |
MongoMapper.database = 'mm_test' | |
class Category | |
include MongoMapper::Document |
This file contains hidden or 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
map = <<-MAP | |
function() { | |
emit(this['_id'], {delta: (this.votes || 0) - (this.yeas || 0)}); | |
} | |
MAP | |
reduce = <<-REDUCE | |
function(key, values) { | |
sum = 0; | |
values.forEach(function(value) { |
This file contains hidden or 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
{"rp": | |
{"AdSpot": [ | |
{"1": {"a": "b"}}, | |
{"2": {"a": "b"}}, | |
{"3": {"a": "b"}}] | |
} | |
} | |
db.collection.find({"rp.AdSpot": {$in: [{"1": {"a": "b"}}, {"2": {"a": "b"}]}}) |
This file contains hidden or 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
> | |
> db.set.insert({_id: {timestamp: 1, contract_id: 1}}) | |
> db.set.insert({_id: {timestamp: 1, contract_id: 10}}) | |
> db.set.insert({_id: {timestamp: 1, contract_id: 20}}) | |
> db.set.insert({_id: {timestamp: 1, contract_id: 50}}) | |
> db.set.find() | |
{ "_id" : { "timestamp" : 1, "contract_id" : 1 } } | |
{ "_id" : { "timestamp" : 1, "contract_id" : 10 } } | |
{ "_id" : { "timestamp" : 1, "contract_id" : 20 } } | |
{ "_id" : { "timestamp" : 1, "contract_id" : 50 } } |
This file contains hidden or 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
require 'rubygems' | |
require 'mongo' | |
con = Mongo::Connection.new | |
col = con['database_name']['collection_name'] | |
col.find({'_id' => {'$type' => 2}}).each do |doc| | |
doc['_id'] = Mongo::ObjectID.from_string(doc['_id']) | |
col.insert(doc) | |
end |
This file contains hidden or 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
# This works. Using MongoDB 1.3.3 | |
require 'rubygems' | |
require 'mongo' | |
# Simple assert method | |
def assert | |
raise "Assertion failed" unless yield | |
end |
This file contains hidden or 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
require 'rubygems' | |
require 'mongo' | |
con = Mongo::Connection.new | |
db = con.db('threads') | |
col = db['resources'] | |
col.remove | |
10.times do |n| | |
col.insert(:a => n) |
This file contains hidden or 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
## | |
# shoulda-mini | |
# based on test/spec/mini 5 | |
# http://gist.github.com/307649 | |
# [email protected] | |
# | |
def context(*args, &block) | |
return super unless (name = args.first) && block | |
require 'test/unit' | |
klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do |
This file contains hidden or 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
@product = {"name" => "NoSQL Rules", | |
"_id" => "ae3f3dc0001", | |
"categories" => ["ae3f3dc0001", "ae3f3dc0002"] | |
} | |
@category = {"_id" => "ae3f3dc0001", | |
"name" => "Technology" | |
} | |
This file contains hidden or 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
rrequire 'rubygems' | |
require 'mongo_mapper' | |
MongoMapper.database = 'case' | |
class User | |
include MongoMapper::Document | |
key :username, String | |
validates_uniqueness_of :username, :case_sensitive => true | |
end |