Skip to content

Instantly share code, notes, and snippets.

@duien
Created November 11, 2009 20:32
Show Gist options
  • Save duien/232266 to your computer and use it in GitHub Desktop.
Save duien/232266 to your computer and use it in GitHub Desktop.
require 'mongo'
describe "MongoDB" do
before(:all) do
@db = Mongo::Connection.new.db('test').collection('index_test')
@db.drop
@db.create_index( 'a' )
@db.insert( { 'a' => 1 } )
@db.insert( { 'b' => 1 } )
end
it "should find a record where indexed key does not exist" do
@db.find_one( 'a' => { '$exists' => false } ).should_not be_nil
end
it "should find a record where indexed key exists" do
@db.find_one( 'a' => { '$exists' => true } ).should_not be_nil
end
it "should find a record where unindexed key does not exist" do
@db.find_one( 'b' => { '$exists' => false } ).should_not be_nil
end
it "should find a record where unindexed key exists" do
@db.find_one( 'b' => { '$exists' => true } ).should_not be_nil
end
end
MongoDB
- should find a record where indexed key does not exist (FAILED - 1)
- should find a record where indexed key exists (FAILED - 2)
- should find a record where unindexed key does not exist
- should find a record where unindexed key exists
1)
'MongoDB should find a record where indexed key does not exist' FAILED
expected not nil, got nil
./mongo_problem_spec.rb:14:
2)
'MongoDB should find a record where indexed key exists' FAILED
expected not nil, got nil
./mongo_problem_spec.rb:18:
Finished in 5.018523 seconds
4 examples, 2 failures
> db.index_test.drop()
{"nIndexesWas" : 2 , "msg" : "all indexes deleted for collection" , "ns" : "test.index_test" , "ok" : 1}
> db.index_test.ensureIndex({'a':1})
true
> db.index_test.save({'a':1})
> db.index_test.save({'b':1})
> db.index_test.find({'a':{$exists:true}})
> db.index_test.find({'a':{$exists:false}})
> db.index_test.find({'b':{$exists:true}})
{"_id" : ObjectId( "4afb1cf16dc9bf5d3b820748") , "b" : 1}
> db.index_test.find({'b':{$exists:false}})
{"_id" : ObjectId( "4afb1ced6dc9bf5d3b820747") , "a" : 1}
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment