Skip to content

Commit c4e0c46

Browse files
committed
RUBY-945 Add list_databases and list_collections helpers
1 parent 300cb5b commit c4e0c46

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

lib/mongo/client.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,18 @@ def write_concern
232232
#
233233
# @since 2.0.5
234234
def database_names
235-
use(Database::ADMIN).command(
236-
listDatabases: 1
237-
).first['databases'].collect{ |info| info['name'] }
235+
list_databases.collect{ |info| info['name'] }
238236
end
239237

240238
# Get info for each database.
241239
#
242-
# @example Get the database info.
243-
# client.database_info
240+
# @example Get the info for each database.
241+
# client.list_databases
244242
#
245-
# @return [ Array<Hash ] The info for each database.
243+
# @return [ Array<Hash> ] The info for each database.
246244
#
247245
# @since 2.0.5
248-
def database_info
246+
def list_databases
249247
use(Database::ADMIN).command(
250248
listDatabases: 1
251249
).first['databases']

lib/mongo/database.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ def collection_names(options = {})
102102
View.new(self).collection_names(options)
103103
end
104104

105+
# Get info on all the collections in the database.
106+
#
107+
# @example Get info on each collection.
108+
# database.list_collections
109+
#
110+
# @return [ Array<Hash> ] Info for each collection in the database.
111+
#
112+
# @since 2.0.5
113+
def list_collections
114+
View.new(self).list_collections
115+
end
116+
105117
# Get all the collections that belong to this database.
106118
#
107119
# @example Get all the collections.

lib/mongo/database/view.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ def collection_names(options = {})
5757
end
5858
end
5959

60+
# Get info on all the collections in the database.
61+
#
62+
# @example Get info on each collection.
63+
# database.list_collections
64+
#
65+
# @return [ Array<Hash> ] Info for each collection in the database.
66+
#
67+
# @since 2.0.5
68+
def list_collections
69+
collections_info(next_primary)
70+
end
71+
6072
# Create the new database view.
6173
#
6274
# @example Create the new database view.

spec/mongo/client_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,11 @@
579579
end
580580
end
581581

582-
describe '#database_info' do
582+
describe '#list_databases' do
583583

584584
it 'returns a list of database info documents' do
585585
expect(
586-
root_authorized_client.database_info.collect do |i|
586+
root_authorized_client.list_databases.collect do |i|
587587
i['name']
588588
end).to include('admin', 'local', TEST_DB)
589589
end

spec/mongo/database_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@
9595
end
9696
end
9797

98+
describe '#list_collections' do
99+
100+
let(:database) do
101+
described_class.new(authorized_client, TEST_DB)
102+
end
103+
104+
let(:result) do
105+
database.list_collections.map do |info|
106+
info['name']
107+
end
108+
end
109+
110+
before do
111+
database[:users].create
112+
end
113+
114+
after do
115+
database[:users].drop
116+
end
117+
118+
it 'returns a list of the collections info', if: write_command_enabled? do
119+
expect(result).to include('users')
120+
end
121+
122+
it 'returns a list of the collections info', unless: write_command_enabled? do
123+
expect(result).to include("#{TEST_DB}.users")
124+
end
125+
end
126+
98127
describe '#collections' do
99128

100129
context 'when the database exists' do

0 commit comments

Comments
 (0)