mongo 0.15 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +3 -1
- data/lib/mongo.rb +1 -1
- data/lib/mongo/collection.rb +3 -3
- data/lib/mongo/connection.rb +4 -0
- data/lib/mongo/cursor.rb +13 -5
- data/lib/mongo/db.rb +3 -0
- data/lib/mongo/errors.rb +3 -0
- data/lib/mongo/message/query_message.rb +11 -20
- data/lib/mongo/query.rb +1 -1
- data/lib/mongo/util/conversions.rb +110 -0
- data/mongo-ruby-driver.gemspec +3 -1
- data/test/test_connection.rb +11 -0
- data/test/test_conversions.rb +121 -0
- data/test/test_cursor.rb +15 -7
- data/test/test_db.rb +13 -0
- data/test/test_db_api.rb +12 -19
- metadata +4 -2
data/README.rdoc
CHANGED
@@ -311,6 +311,7 @@ Jon Crosby, jon@joncrosby.me
|
|
311
311
|
John Nunemaker, http://railstips.org
|
312
312
|
* Collection#create_index takes symbols as well as strings
|
313
313
|
* Fix for Collection#save
|
314
|
+
* Add logger convenience methods to connection and database
|
314
315
|
|
315
316
|
David James, djames@sunlightfoundation.com
|
316
317
|
* Fix dates to return as UTC
|
@@ -319,8 +320,9 @@ Paul Dlug, paul.dlug@gmail.com
|
|
319
320
|
* Generate _id on the client side if not provided
|
320
321
|
* Collection#insert and Collection#save return _id
|
321
322
|
|
322
|
-
Durran Jordan
|
323
|
+
Durran Jordan, durran@gmail.com
|
323
324
|
* DB#collections
|
325
|
+
* Support for specifying sort order as array of [key, direction] pairs
|
324
326
|
|
325
327
|
Cyril Mougel, cyril.mougel@gmail.com
|
326
328
|
* Initial logging support
|
data/lib/mongo.rb
CHANGED
data/lib/mongo/collection.rb
CHANGED
@@ -91,9 +91,9 @@ module Mongo
|
|
91
91
|
# :skip :: Number of documents to omit (from the start of the result set)
|
92
92
|
# when returning the results
|
93
93
|
# :limit :: Maximum number of records to return
|
94
|
-
# :sort ::
|
95
|
-
#
|
96
|
-
#
|
94
|
+
# :sort :: An array of [key, direction] pairs to sort by. Direction should
|
95
|
+
# be specified as Mongo::ASCENDING (or :ascending / :asc) or
|
96
|
+
# Mongo::DESCENDING (or :descending / :desc)
|
97
97
|
# :hint :: See #hint. This option overrides the collection-wide value.
|
98
98
|
# :snapshot :: If true, snapshot mode will be used for this query.
|
99
99
|
# Snapshot mode assures no duplicates are returned, or
|
data/lib/mongo/connection.rb
CHANGED
data/lib/mongo/cursor.rb
CHANGED
@@ -75,16 +75,24 @@ module Mongo
|
|
75
75
|
|
76
76
|
# Sort this cursor's result
|
77
77
|
#
|
78
|
-
# Takes either a
|
79
|
-
#
|
80
|
-
#
|
78
|
+
# Takes either a single key and a direction, or an array of [key,
|
79
|
+
# direction] pairs. Directions should be specified as Mongo::ASCENDING
|
80
|
+
# or Mongo::DESCENDING (or :ascending or :descending) (or :asc or :desc).
|
81
81
|
#
|
82
|
-
# Raises InvalidOperation if this cursor has already been used.
|
82
|
+
# Raises InvalidOperation if this cursor has already been used. Raises
|
83
|
+
# InvalidSortValueError if specified order is invalid.
|
83
84
|
#
|
84
85
|
# This method overrides any sort order specified in the Collection#find
|
85
86
|
# method, and only the last sort applied has an effect
|
86
|
-
def sort(
|
87
|
+
def sort(key_or_list, direction=nil)
|
87
88
|
check_modifiable
|
89
|
+
|
90
|
+
if !direction.nil?
|
91
|
+
order = [[key_or_list, direction]]
|
92
|
+
else
|
93
|
+
order = key_or_list
|
94
|
+
end
|
95
|
+
|
88
96
|
@query.order_by = order
|
89
97
|
self
|
90
98
|
end
|
data/lib/mongo/db.rb
CHANGED
@@ -58,6 +58,9 @@ module Mongo
|
|
58
58
|
|
59
59
|
# The database's socket. For internal (and Cursor) use only.
|
60
60
|
attr_reader :socket
|
61
|
+
|
62
|
+
# The logger instance if :logger is passed to initialize
|
63
|
+
attr_reader :logger
|
61
64
|
|
62
65
|
def slave_ok?; @slave_ok; end
|
63
66
|
def auto_reconnect?; @auto_reconnect; end
|
data/lib/mongo/errors.rb
CHANGED
@@ -16,11 +16,12 @@
|
|
16
16
|
|
17
17
|
require 'mongo/message/message'
|
18
18
|
require 'mongo/message/opcodes'
|
19
|
+
require 'mongo/util/conversions'
|
19
20
|
require 'mongo/util/ordered_hash'
|
20
21
|
|
21
22
|
module Mongo
|
22
|
-
|
23
23
|
class QueryMessage < Message
|
24
|
+
include Mongo::Conversions
|
24
25
|
|
25
26
|
attr_reader :query
|
26
27
|
|
@@ -36,27 +37,17 @@ module Mongo
|
|
36
37
|
if query.contains_special_fields
|
37
38
|
sel = OrderedHash.new
|
38
39
|
sel['query'] = query.selector
|
39
|
-
if query.order_by
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
when
|
44
|
-
|
45
|
-
query.order_by.each { |ob|
|
46
|
-
case ob
|
47
|
-
when String
|
48
|
-
h[ob] = 1
|
49
|
-
when Hash # should have one entry; will handle all
|
50
|
-
ob.each { |k,v| h[k] = v }
|
51
|
-
else
|
52
|
-
raise "illegal query order_by value #{query.order_by.inspect}"
|
53
|
-
end
|
54
|
-
}
|
55
|
-
h
|
40
|
+
if query.order_by
|
41
|
+
order_by = query.order_by
|
42
|
+
sel['orderby'] = case order_by
|
43
|
+
when String then string_as_sort_parameters(order_by)
|
44
|
+
when Symbol then symbol_as_sort_parameters(order_by)
|
45
|
+
when Array then array_as_sort_parameters(order_by)
|
56
46
|
when Hash # Should be an ordered hash, but this message doesn't care
|
57
|
-
|
47
|
+
warn_if_deprecated(order_by)
|
48
|
+
order_by
|
58
49
|
else
|
59
|
-
raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
|
50
|
+
raise InvalidSortValueError.new("illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash")
|
60
51
|
end
|
61
52
|
end
|
62
53
|
sel['$hint'] = query.hint if query.hint && query.hint.length > 0
|
data/lib/mongo/query.rb
CHANGED
@@ -0,0 +1,110 @@
|
|
1
|
+
# --
|
2
|
+
# Copyright (C) 2008-2009 10gen Inc.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
# ++
|
16
|
+
module Mongo #:nodoc:
|
17
|
+
|
18
|
+
# Utility module to include when needing to convert certain types of
|
19
|
+
# objects to mongo-friendly parameters.
|
20
|
+
module Conversions
|
21
|
+
|
22
|
+
ASCENDING = ["ascending", "asc", "1"]
|
23
|
+
DESCENDING = ["descending", "desc", "-1"]
|
24
|
+
|
25
|
+
# Converts the supplied +Array+ to a +Hash+ to pass to mongo as
|
26
|
+
# sorting parameters. The returned +Hash+ will vary depending
|
27
|
+
# on whether the passed +Array+ is one or two dimensional.
|
28
|
+
#
|
29
|
+
# Example:
|
30
|
+
#
|
31
|
+
# *DEPRECATED
|
32
|
+
#
|
33
|
+
# <tt>array_as_sort_parameters(["field1", "field2"])</tt> =>
|
34
|
+
# <tt>{ "field1" => "1", "field2" => "1" }</tt>
|
35
|
+
#
|
36
|
+
# *New Syntax:
|
37
|
+
#
|
38
|
+
# <tt>array_as_sort_parameters([["field1", :asc], ["field2", :desc]])</tt> =>
|
39
|
+
# <tt>{ "field1" => 1, "field2" => -1}</tt>
|
40
|
+
def array_as_sort_parameters(value)
|
41
|
+
warn_if_deprecated(value)
|
42
|
+
order_by = OrderedHash.new
|
43
|
+
value.each do |param|
|
44
|
+
if (param.class.name == "String")
|
45
|
+
order_by[param] = 1
|
46
|
+
else
|
47
|
+
order_by[param[0]] = sort_value(param[1]) unless param[1].nil?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
order_by
|
51
|
+
end
|
52
|
+
|
53
|
+
# Converts the supplied +String+ to a +Hash+ to pass to mongo as
|
54
|
+
# a sorting parameter with ascending order. If the +String+
|
55
|
+
# is empty then an empty +Hash+ will be returned.
|
56
|
+
#
|
57
|
+
# Example:
|
58
|
+
#
|
59
|
+
# *DEPRECATED
|
60
|
+
#
|
61
|
+
# <tt>string_as_sort_parameters("field")</tt> => <tt>{ "field" => 1 }</tt>
|
62
|
+
# <tt>string_as_sort_parameters("")</tt> => <tt>{}</tt>
|
63
|
+
def string_as_sort_parameters(value)
|
64
|
+
warn_if_deprecated(value)
|
65
|
+
return {} if value.empty?
|
66
|
+
{ value => 1 }
|
67
|
+
end
|
68
|
+
|
69
|
+
# Converts the supplied +Symbol+ to a +Hash+ to pass to mongo as
|
70
|
+
# a sorting parameter with ascending order.
|
71
|
+
#
|
72
|
+
# Example:
|
73
|
+
#
|
74
|
+
# *DEPRECATED
|
75
|
+
#
|
76
|
+
# <tt>symbol_as_sort_parameters(:field)</tt> => <tt>{ "field" => 1 }</tt>
|
77
|
+
def symbol_as_sort_parameters(value)
|
78
|
+
warn_if_deprecated(value)
|
79
|
+
{ value.to_s => 1 }
|
80
|
+
end
|
81
|
+
|
82
|
+
# Converts the +String+, +Symbol+, or +Integer+ to the
|
83
|
+
# corresponding sort value in MongoDB.
|
84
|
+
#
|
85
|
+
# Valid conversions (case-insensitive):
|
86
|
+
#
|
87
|
+
# <tt>ascending, asc, :ascending, :asc, 1</tt> => <tt>1</tt>
|
88
|
+
# <tt>descending, desc, :descending, :desc, -1</tt> => <tt>-1</tt>
|
89
|
+
#
|
90
|
+
# If the value is invalid then an error will be raised.
|
91
|
+
def sort_value(value)
|
92
|
+
val = value.to_s.downcase
|
93
|
+
return 1 if ASCENDING.include?(val)
|
94
|
+
return -1 if DESCENDING.include?(val)
|
95
|
+
raise InvalidSortValueError.new(
|
96
|
+
"#{self} was supplied as a sort direction when acceptable values are: " +
|
97
|
+
"Mongo::ASCENDING, 'ascending', 'asc', :ascending, :asc, 1, Mongo::DESCENDING, 'descending', 'desc', :descending, :desc, -1.")
|
98
|
+
end
|
99
|
+
|
100
|
+
# This is the method to call when the client needs to be warned of
|
101
|
+
# deprecation in the way sorting parameters are supplied.
|
102
|
+
def warn_if_deprecated(value)
|
103
|
+
unless value.is_a?(Array) && value.first.is_a?(Array)
|
104
|
+
warn("Specifying sort order as #{value.inspect} has been deprecated in favor of a new syntax: \n" +
|
105
|
+
" :sort => [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
data/mongo-ruby-driver.gemspec
CHANGED
@@ -45,6 +45,7 @@ PACKAGE_FILES = ['README.rdoc', 'Rakefile', 'mongo-ruby-driver.gemspec',
|
|
45
45
|
'lib/mongo/types/regexp_of_holding.rb',
|
46
46
|
'lib/mongo/util/bson.rb',
|
47
47
|
'lib/mongo/util/byte_buffer.rb',
|
48
|
+
'lib/mongo/util/conversions.rb',
|
48
49
|
'lib/mongo/util/ordered_hash.rb',
|
49
50
|
'lib/mongo/util/xml_to_ruby.rb',
|
50
51
|
'lib/mongo.rb']
|
@@ -68,6 +69,7 @@ TEST_FILES = ['test/mongo-qa/_common.rb',
|
|
68
69
|
'test/test_chunk.rb',
|
69
70
|
'test/test_collection.rb',
|
70
71
|
'test/test_connection.rb',
|
72
|
+
'test/test_conversions.rb',
|
71
73
|
'test/test_cursor.rb',
|
72
74
|
'test/test_db.rb',
|
73
75
|
'test/test_db_api.rb',
|
@@ -83,7 +85,7 @@ Gem::Specification.new do |s|
|
|
83
85
|
s.name = 'mongo'
|
84
86
|
|
85
87
|
# be sure to change this constant in lib/mongo.rb as well
|
86
|
-
s.version = '0.15'
|
88
|
+
s.version = '0.15.1'
|
87
89
|
|
88
90
|
s.platform = Gem::Platform::RUBY
|
89
91
|
s.summary = 'Ruby driver for the MongoDB'
|
data/test/test_connection.rb
CHANGED
@@ -66,6 +66,17 @@ class TestConnection < Test::Unit::TestCase
|
|
66
66
|
assert output.string.include?("db.test.find")
|
67
67
|
assert !output.string.include?("db.test.remove")
|
68
68
|
end
|
69
|
+
|
70
|
+
def test_connection_logger
|
71
|
+
output = StringIO.new
|
72
|
+
logger = Logger.new(output)
|
73
|
+
logger.level = Logger::DEBUG
|
74
|
+
connection = Connection.new(@host, @port, :logger => logger)
|
75
|
+
assert_equal logger, connection.logger
|
76
|
+
|
77
|
+
connection.logger.debug 'testing'
|
78
|
+
assert output.string.include?('testing')
|
79
|
+
end
|
69
80
|
|
70
81
|
def test_drop_database
|
71
82
|
db = @mongo.db('ruby-mongo-will-be-deleted')
|
@@ -0,0 +1,121 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo/errors'
|
3
|
+
require 'mongo/util/conversions'
|
4
|
+
require 'mongo/util/ordered_hash'
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class ConversionsTest < Test::Unit::TestCase
|
8
|
+
include Mongo::Conversions
|
9
|
+
|
10
|
+
def test_array_as_sort_parameters_with_array_of_strings
|
11
|
+
params = array_as_sort_parameters(["field1", "field2"])
|
12
|
+
assert_equal({ "field1" => 1, "field2" => 1 }, params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_array_as_sort_parameters_with_array_of_string_and_values
|
16
|
+
params = array_as_sort_parameters([["field1", :asc], ["field2", :desc]])
|
17
|
+
assert_equal({ "field1" => 1, "field2" => -1 }, params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_string_as_sort_parameters_with_string
|
21
|
+
params = string_as_sort_parameters("field")
|
22
|
+
assert_equal({ "field" => 1 }, params)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_string_as_sort_parameters_with_empty_string
|
26
|
+
params = string_as_sort_parameters("")
|
27
|
+
assert_equal({}, params)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_symbol_as_sort_parameters
|
31
|
+
params = symbol_as_sort_parameters(:field)
|
32
|
+
assert_equal({ "field" => 1 }, params)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_sort_value_when_value_is_one
|
36
|
+
assert_equal 1, sort_value(1)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_sort_value_when_value_is_one_as_a_string
|
40
|
+
assert_equal 1, sort_value("1")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_sort_value_when_value_is_negative_one
|
44
|
+
assert_equal -1, sort_value(-1)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_sort_value_when_value_is_negative_one_as_a_string
|
48
|
+
assert_equal -1, sort_value("-1")
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_sort_value_when_value_is_ascending
|
52
|
+
assert_equal 1, sort_value("ascending")
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_sort_value_when_value_is_asc
|
56
|
+
assert_equal 1, sort_value("asc")
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_sort_value_when_value_is_uppercase_ascending
|
60
|
+
assert_equal 1, sort_value("ASCENDING")
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_sort_value_when_value_is_uppercase_asc
|
64
|
+
assert_equal 1, sort_value("ASC")
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_sort_value_when_value_is_symbol_ascending
|
68
|
+
assert_equal 1, sort_value(:ascending)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_sort_value_when_value_is_symbol_asc
|
72
|
+
assert_equal 1, sort_value(:asc)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_sort_value_when_value_is_symbol_uppercase_ascending
|
76
|
+
assert_equal 1, sort_value(:ASCENDING)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_sort_value_when_value_is_symbol_uppercase_asc
|
80
|
+
assert_equal 1, sort_value(:ASC)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_sort_value_when_value_is_descending
|
84
|
+
assert_equal -1, sort_value("descending")
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_sort_value_when_value_is_desc
|
88
|
+
assert_equal -1, sort_value("desc")
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_sort_value_when_value_is_uppercase_descending
|
92
|
+
assert_equal -1, sort_value("DESCENDING")
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_sort_value_when_value_is_uppercase_desc
|
96
|
+
assert_equal -1, sort_value("DESC")
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_sort_value_when_value_is_symbol_descending
|
100
|
+
assert_equal -1, sort_value(:descending)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_sort_value_when_value_is_symbol_desc
|
104
|
+
assert_equal -1, sort_value(:desc)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_sort_value_when_value_is_uppercase_symbol_descending
|
108
|
+
assert_equal -1, sort_value(:DESCENDING)
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_sort_value_when_value_is_uppercase_symbol_desc
|
112
|
+
assert_equal -1, sort_value(:DESC)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_sort_value_when_value_is_invalid
|
116
|
+
assert_raise Mongo::InvalidSortValueError do
|
117
|
+
sort_value(2)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
data/test/test_cursor.rb
CHANGED
@@ -62,22 +62,30 @@ class CursorTest < Test::Unit::TestCase
|
|
62
62
|
@@coll.clear
|
63
63
|
5.times{|x| @@coll.insert({"a" => x}) }
|
64
64
|
|
65
|
-
assert_kind_of Cursor, @@coll.find().sort(
|
65
|
+
assert_kind_of Cursor, @@coll.find().sort(:a, 1)
|
66
66
|
|
67
|
-
assert_equal 0, @@coll.find().sort(
|
68
|
-
assert_equal 4, @@coll.find().sort(
|
69
|
-
assert_equal 0, @@coll.find().sort(["a"]).next_object["a"]
|
67
|
+
assert_equal 0, @@coll.find().sort(:a, 1).next_object["a"]
|
68
|
+
assert_equal 4, @@coll.find().sort(:a, -1).next_object["a"]
|
69
|
+
assert_equal 0, @@coll.find().sort([["a", :asc]]).next_object["a"]
|
70
70
|
|
71
|
-
assert_kind_of Cursor, @@coll.find().sort(
|
71
|
+
assert_kind_of Cursor, @@coll.find().sort([[:a, -1], [:b, 1]])
|
72
72
|
|
73
|
-
assert_equal 4, @@coll.find().sort(
|
74
|
-
assert_equal 0, @@coll.find().sort(
|
73
|
+
assert_equal 4, @@coll.find().sort(:a, 1).sort(:a, -1).next_object["a"]
|
74
|
+
assert_equal 0, @@coll.find().sort(:a, -1).sort(:a, 1).next_object["a"]
|
75
75
|
|
76
76
|
cursor = @@coll.find()
|
77
77
|
cursor.next_object()
|
78
78
|
assert_raise InvalidOperation do
|
79
79
|
cursor.sort(["a"])
|
80
80
|
end
|
81
|
+
|
82
|
+
assert_raise InvalidSortValueError do
|
83
|
+
@@coll.find().sort(:a, 25).next_object
|
84
|
+
end
|
85
|
+
|
86
|
+
assert_raise InvalidSortValueError do
|
87
|
+
@@coll.find().sort(25).next_object
|
88
|
+
end
|
81
89
|
end
|
82
90
|
|
83
91
|
def test_limit
|
data/test/test_db.rb
CHANGED
@@ -2,6 +2,8 @@ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
2
2
|
require 'digest/md5'
|
3
3
|
require 'mongo'
|
4
4
|
require 'test/unit'
|
5
|
+
require 'stringio'
|
6
|
+
require 'logger'
|
5
7
|
|
6
8
|
class TestPKFactory
|
7
9
|
def create_pk(row)
|
@@ -45,6 +47,17 @@ class DBTest < Test::Unit::TestCase
|
|
45
47
|
@@users = @@db.collection('system.users')
|
46
48
|
end
|
47
49
|
end
|
50
|
+
|
51
|
+
def test_logger
|
52
|
+
output = StringIO.new
|
53
|
+
logger = Logger.new(output)
|
54
|
+
logger.level = Logger::DEBUG
|
55
|
+
db = Connection.new(@host, @port, :logger => logger).db('ruby-mongo-test')
|
56
|
+
assert_equal logger, db.logger
|
57
|
+
|
58
|
+
db.logger.debug 'testing'
|
59
|
+
assert output.string.include?('testing')
|
60
|
+
end
|
48
61
|
|
49
62
|
def test_full_coll_name
|
50
63
|
coll = @@db.collection('test')
|
data/test/test_db_api.rb
CHANGED
@@ -149,7 +149,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
149
149
|
@@coll.insert('a' => 4, 'b' => 1)
|
150
150
|
|
151
151
|
# Sorting (ascending)
|
152
|
-
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort =>
|
152
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => [['a', 1]]).to_a
|
153
153
|
assert_equal 4, docs.size
|
154
154
|
assert_equal 1, docs[0]['a']
|
155
155
|
assert_equal 2, docs[1]['a']
|
@@ -157,7 +157,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
157
157
|
assert_equal 4, docs[3]['a']
|
158
158
|
|
159
159
|
# Sorting (descending)
|
160
|
-
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort =>
|
160
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => [['a', -1]]).to_a
|
161
161
|
assert_equal 4, docs.size
|
162
162
|
assert_equal 4, docs[0]['a']
|
163
163
|
assert_equal 3, docs[1]['a']
|
@@ -187,16 +187,10 @@ class DBAPITest < Test::Unit::TestCase
|
|
187
187
|
assert_equal 1, docs[2]['a']
|
188
188
|
assert_equal 3, docs[3]['a']
|
189
189
|
|
190
|
-
# Sorting using empty array; no order guarantee
|
191
|
-
# should not blow up.
|
190
|
+
# Sorting using empty array; no order guarantee should not blow up.
|
192
191
|
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
|
193
192
|
assert_equal 4, docs.size
|
194
193
|
|
195
|
-
# Sorting using array of hashes; no order guarantee (Mongo bug #898) but
|
196
|
-
# should not blow up.
|
197
|
-
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => [{'b' => 1}, {'a' => -1}]).to_a
|
198
|
-
assert_equal 4, docs.size
|
199
|
-
|
200
194
|
# Sorting using ordered hash. You can use an unordered one, but then the
|
201
195
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
202
196
|
oh = OrderedHash.new
|
@@ -208,16 +202,15 @@ class DBAPITest < Test::Unit::TestCase
|
|
208
202
|
assert_equal 2, docs[2]['a']
|
209
203
|
assert_equal 1, docs[3]['a']
|
210
204
|
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
# assert_equal 4, docs[3]['a']
|
205
|
+
oh = OrderedHash.new
|
206
|
+
oh['b'] = -1
|
207
|
+
oh['a'] = 1
|
208
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
209
|
+
assert_equal 4, docs.size
|
210
|
+
assert_equal 1, docs[0]['a']
|
211
|
+
assert_equal 3, docs[1]['a']
|
212
|
+
assert_equal 2, docs[2]['a']
|
213
|
+
assert_equal 4, docs[3]['a']
|
221
214
|
end
|
222
215
|
|
223
216
|
def test_find_limits
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.15.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Menard
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-10-
|
13
|
+
date: 2009-10-08 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- lib/mongo/types/regexp_of_holding.rb
|
71
71
|
- lib/mongo/util/bson.rb
|
72
72
|
- lib/mongo/util/byte_buffer.rb
|
73
|
+
- lib/mongo/util/conversions.rb
|
73
74
|
- lib/mongo/util/ordered_hash.rb
|
74
75
|
- lib/mongo/util/xml_to_ruby.rb
|
75
76
|
- lib/mongo.rb
|
@@ -124,6 +125,7 @@ test_files:
|
|
124
125
|
- test/test_chunk.rb
|
125
126
|
- test/test_collection.rb
|
126
127
|
- test/test_connection.rb
|
128
|
+
- test/test_conversions.rb
|
127
129
|
- test/test_cursor.rb
|
128
130
|
- test/test_db.rb
|
129
131
|
- test/test_db_api.rb
|