Merge branch 'master' of git://github.com/jimm/mongo-ruby-driver
This commit is contained in:
commit
40fbb501cb
2
README
2
README
|
@ -57,8 +57,6 @@ type
|
||||||
|
|
||||||
* Capped collection support.
|
* Capped collection support.
|
||||||
|
|
||||||
* Implement order_by (right now, sort order is ignored)
|
|
||||||
|
|
||||||
* More code comments. More text in this file.
|
* More code comments. More text in this file.
|
||||||
|
|
||||||
* Support more types: REF, SYMBOL, CODE_W_SCOPE, etc.
|
* Support more types: REF, SYMBOL, CODE_W_SCOPE, etc.
|
||||||
|
|
|
@ -39,7 +39,9 @@ module XGen
|
||||||
|
|
||||||
def next_object
|
def next_object
|
||||||
refill_via_get_more if num_remaining == 0
|
refill_via_get_more if num_remaining == 0
|
||||||
@objects.shift
|
o = @objects.shift
|
||||||
|
raise o['$err'] if o['$err']
|
||||||
|
o
|
||||||
end
|
end
|
||||||
|
|
||||||
def each
|
def each
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
require 'mongo/message/message'
|
require 'mongo/message/message'
|
||||||
require 'mongo/message/opcodes'
|
require 'mongo/message/opcodes'
|
||||||
|
require 'mongo/util/ordered_hash'
|
||||||
|
|
||||||
module XGen
|
module XGen
|
||||||
module Mongo
|
module Mongo
|
||||||
|
@ -13,7 +14,30 @@ module XGen
|
||||||
write_string("#{db_name}.#{collection_name}")
|
write_string("#{db_name}.#{collection_name}")
|
||||||
write_int(query.number_to_skip)
|
write_int(query.number_to_skip)
|
||||||
write_int(query.number_to_return)
|
write_int(query.number_to_return)
|
||||||
write_doc(query.selector)
|
sel = query.selector
|
||||||
|
if query.order_by
|
||||||
|
sel = OrderedHash.new
|
||||||
|
sel['query'] = query.selector
|
||||||
|
sel['orderby'] = case query.order_by
|
||||||
|
when Array
|
||||||
|
if query.order_by.empty? # Empty array of order_by values
|
||||||
|
[]
|
||||||
|
else
|
||||||
|
case query.order_by[0]
|
||||||
|
when Hash # Array of hashes
|
||||||
|
query.order_by
|
||||||
|
else # ['a', 'b']
|
||||||
|
query.order_by.collect { |v| {v => 1} } # Assume ascending order for all values
|
||||||
|
end
|
||||||
|
end
|
||||||
|
when Hash # Should be an ordered hash, but this message doesn't care
|
||||||
|
a = []
|
||||||
|
query.order_by.each { |k,v| a << {k => v }}
|
||||||
|
a
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
write_doc(sel)
|
||||||
write_doc(query.fields) if query.fields
|
write_doc(query.fields) if query.fields
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -124,16 +124,30 @@ class DBAPITest < Test::Unit::TestCase
|
||||||
@coll.insert('b' => 3)
|
@coll.insert('b' => 3)
|
||||||
|
|
||||||
# Sorting (ascending)
|
# Sorting (ascending)
|
||||||
order_by = OrderedHash.new
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'a' => 1}]).map
|
||||||
order_by['a'] = 1
|
|
||||||
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => order_by).map
|
|
||||||
assert_equal 2, docs.size
|
assert_equal 2, docs.size
|
||||||
assert_equal 1, docs.first['a']
|
assert_equal 1, docs.first['a']
|
||||||
|
|
||||||
# Sorting (descending)
|
# Sorting (descending)
|
||||||
order_by = OrderedHash.new
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'a' => -1}]).map
|
||||||
order_by['a'] = -1
|
assert_equal 2, docs.size
|
||||||
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => order_by).map
|
assert_equal 2, docs.first['a']
|
||||||
|
|
||||||
|
# Sorting using array of names; assumes ascending order
|
||||||
|
# NOTE: Mongo itself does not yet properly sort by multiple names
|
||||||
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).map
|
||||||
|
assert_equal 2, docs.size
|
||||||
|
assert_equal 1, docs.first['a']
|
||||||
|
|
||||||
|
# Sorting using empty array; no order guarantee but should not blow up.
|
||||||
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'a' => -1}]).map
|
||||||
|
assert_equal 2, docs.size
|
||||||
|
|
||||||
|
# Sorting using ordered hash. You can use an unordered one, but then the
|
||||||
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
||||||
|
oh = OrderedHash.new
|
||||||
|
oh['a'] = -1
|
||||||
|
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).map
|
||||||
assert_equal 2, docs.size
|
assert_equal 2, docs.size
|
||||||
assert_equal 2, docs.first['a']
|
assert_equal 2, docs.first['a']
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue