Skip to content

Commit 95a829a

Browse files
committed
Add ListDatabases Filter and NameOnly options and ListDatabaseNames functionality
- Add ListDatabasesOptions class - Update base classes and interfaces to support ListDatabasesOptions - Add tests for filter option - Add nameOnly tests - Add ListDatabaseNames tests
1 parent bc9e75f commit 95a829a

File tree

11 files changed

+637
-32
lines changed

11 files changed

+637
-32
lines changed

src/MongoDB.Driver.Core/Core/Misc/Feature.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class Feature
4747
private static readonly Feature __findAndModifyWriteConcern = new Feature("FindAndModifyWriteConcern", new SemanticVersion(3, 2, 0));
4848
private static readonly Feature __findCommand = new Feature("FindCommand", new SemanticVersion(3, 2, 0));
4949
private static readonly Feature __listCollectionsCommand = new Feature("ListCollectionsCommand", new SemanticVersion(3, 0, 0));
50+
private static readonly Feature __listDatabasesFilter = new Feature("ListDatabasesFilter", new SemanticVersion(3, 4, 2));
51+
private static readonly Feature __listDatabasesNameOnlyOption = new Feature("ListDatabasesNameOnlyOption", new SemanticVersion(3, 4, 3));
5052
private static readonly Feature __listIndexesCommand = new Feature("ListIndexesCommand", new SemanticVersion(3, 0, 0));
5153
private static readonly Feature __indexOptionsDefaults = new Feature("IndexOptionsDefaults", new SemanticVersion(3, 2, 0));
5254
private static readonly Feature __maxStaleness = new Feature("MaxStaleness", new SemanticVersion(3, 3, 12));
@@ -179,6 +181,16 @@ public class Feature
179181
/// </summary>
180182
public static Feature IndexOptionsDefaults => __indexOptionsDefaults;
181183

184+
/// <summary>
185+
/// Gets the list databases filter feature.
186+
/// </summary>
187+
public static Feature ListDatabasesFilter => __listDatabasesFilter;
188+
189+
/// <summary>
190+
/// Get the list databases nameOnly feature.
191+
/// </summary>
192+
public static Feature ListDatabasesNameOnlyOption => __listDatabasesNameOnlyOption;
193+
182194
/// <summary>
183195
/// Gets the list collections command feature.
184196
/// </summary>

src/MongoDB.Driver.Core/Core/Operations/ListCollectionsOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private IReadOperation<IAsyncCursor<BsonDocument>> CreateOperation(IChannel chan
126126
}
127127
else
128128
{
129-
return new ListCollectionsUsingQueryOperation(_databaseNamespace, _messageEncoderSettings) { Filter = _filter }; ;
129+
return new ListCollectionsUsingQueryOperation(_databaseNamespace, _messageEncoderSettings) { Filter = _filter };
130130
}
131131
}
132132
}

src/MongoDB.Driver.Core/Core/Operations/ListDatabasesOperation.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -32,7 +32,9 @@ namespace MongoDB.Driver.Core.Operations
3232
public class ListDatabasesOperation : IReadOperation<IAsyncCursor<BsonDocument>>
3333
{
3434
// fields
35+
private BsonDocument _filter;
3536
private MessageEncoderSettings _messageEncoderSettings;
37+
private bool? _nameOnly;
3638

3739
// constructors
3840
/// <summary>
@@ -45,6 +47,18 @@ public ListDatabasesOperation(MessageEncoderSettings messageEncoderSettings)
4547
}
4648

4749
// properties
50+
/// <summary>
51+
/// Gets or sets the filter.
52+
/// </summary>
53+
/// <value>
54+
/// The filter.
55+
/// </value>
56+
public BsonDocument Filter
57+
{
58+
get { return _filter; }
59+
set { _filter = value; }
60+
}
61+
4862
/// <summary>
4963
/// Gets the message encoder settings.
5064
/// </summary>
@@ -56,6 +70,18 @@ public MessageEncoderSettings MessageEncoderSettings
5670
get { return _messageEncoderSettings; }
5771
}
5872

73+
/// <summary>
74+
/// Gets or sets the NameOnly flag.
75+
/// </summary>
76+
/// <value>
77+
/// The NameOnly flag.
78+
/// </value>
79+
public bool? NameOnly
80+
{
81+
get { return _nameOnly; }
82+
set { _nameOnly = value; }
83+
}
84+
5985
// public methods
6086
/// <inheritdoc/>
6187
public IAsyncCursor<BsonDocument> Execute(IReadBinding binding, CancellationToken cancellationToken)
@@ -78,7 +104,12 @@ public async Task<IAsyncCursor<BsonDocument>> ExecuteAsync(IReadBinding binding,
78104
// private methods
79105
internal BsonDocument CreateCommand()
80106
{
81-
return new BsonDocument { { "listDatabases", 1 } };
107+
return new BsonDocument
108+
{
109+
{ "listDatabases", 1 },
110+
{ "filter", _filter, _filter != null },
111+
{ "nameOnly", _nameOnly, _nameOnly != null }
112+
};
82113
}
83114

84115
private IAsyncCursor<BsonDocument> CreateCursor(BsonDocument reply)

src/MongoDB.Driver/IMongoClient.cs

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System.Collections.Generic;
1617
using System.Threading;
1718
using System.Threading.Tasks;
1819
using MongoDB.Bson;
@@ -84,42 +85,133 @@ public interface IMongoClient
8485
/// <returns>An implementation of a database.</returns>
8586
IMongoDatabase GetDatabase(string name, MongoDatabaseSettings settings = null);
8687

88+
/// <summary>
89+
/// Returns the names of the databases on the server.
90+
/// </summary>
91+
/// <param name="cancellationToken">The cancellation token.</param>
92+
/// <returns>The database names.</returns>
93+
IAsyncCursor<string> ListDatabaseNames(
94+
CancellationToken cancellationToken = default(CancellationToken));
95+
96+
/// <summary>
97+
/// Returns the names of the databases on the server.
98+
/// </summary>
99+
/// <param name="session">The session.</param>
100+
/// <param name="cancellationToken">The cancellation token.</param>
101+
/// <returns>The database names.</returns>
102+
IAsyncCursor<string> ListDatabaseNames(
103+
IClientSessionHandle session,
104+
CancellationToken cancellationToken = default(CancellationToken));
105+
106+
/// <summary>
107+
/// Returns the names of the databases on the server.
108+
/// </summary>
109+
/// <param name="cancellationToken">The cancellation token.</param>
110+
/// <returns>The database names.</returns>
111+
Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
112+
CancellationToken cancellationToken = default(CancellationToken));
113+
114+
/// <summary>
115+
/// Returns the names of the databases on the server.
116+
/// </summary>
117+
/// <param name="session">The session.</param>
118+
/// <param name="cancellationToken">The cancellation token.</param>
119+
/// <returns>The database names.</returns>
120+
Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
121+
IClientSessionHandle session,
122+
CancellationToken cancellationToken = default(CancellationToken));
123+
87124
/// <summary>
88125
/// Lists the databases on the server.
89126
/// </summary>
90127
/// <param name="cancellationToken">The cancellation token.</param>
91128
/// <returns>A cursor.</returns>
92-
IAsyncCursor<BsonDocument> ListDatabases(CancellationToken cancellationToken = default(CancellationToken));
129+
IAsyncCursor<BsonDocument> ListDatabases(
130+
CancellationToken cancellationToken = default(CancellationToken));
131+
132+
/// <summary>
133+
/// Lists the databases on the server.
134+
/// </summary>
135+
/// <param name="options">The options.</param>
136+
/// <param name="cancellationToken">The cancellation token.</param>
137+
/// <returns>A cursor.</returns>
138+
IAsyncCursor<BsonDocument> ListDatabases(
139+
ListDatabasesOptions options,
140+
CancellationToken cancellationToken = default(CancellationToken));
141+
142+
/// <summary>
143+
/// Lists the databases on the server.
144+
/// </summary>
145+
/// <param name="session">The session.</param>
146+
/// <param name="cancellationToken">The cancellation token.</param>
147+
/// <returns>
148+
/// A cursor.
149+
/// </returns>
150+
IAsyncCursor<BsonDocument> ListDatabases(
151+
IClientSessionHandle session,
152+
CancellationToken cancellationToken = default(CancellationToken));
93153

94154
/// <summary>
95155
/// Lists the databases on the server.
96156
/// </summary>
97157
/// <param name="session">The session.</param>
158+
/// <param name="options">The options.</param>
98159
/// <param name="cancellationToken">The cancellation token.</param>
99160
/// <returns>
100161
/// A cursor.
101162
/// </returns>
102-
IAsyncCursor<BsonDocument> ListDatabases(IClientSessionHandle session, CancellationToken cancellationToken = default(CancellationToken));
163+
IAsyncCursor<BsonDocument> ListDatabases(
164+
IClientSessionHandle session,
165+
ListDatabasesOptions options,
166+
CancellationToken cancellationToken = default(CancellationToken));
167+
168+
/// <summary>
169+
/// Lists the databases on the server.
170+
/// </summary>
171+
/// <param name="cancellationToken">The cancellation token.</param>
172+
/// <returns>A Task whose result is a cursor.</returns>
173+
Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(
174+
CancellationToken cancellationToken = default(CancellationToken));
103175

104176
/// <summary>
105177
/// Lists the databases on the server.
106178
/// </summary>
107179
/// <param name="cancellationToken">The cancellation token.</param>
180+
/// <param name="options">The options.</param>
108181
/// <returns>A Task whose result is a cursor.</returns>
109-
Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(CancellationToken cancellationToken = default(CancellationToken));
182+
Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(
183+
ListDatabasesOptions options,
184+
CancellationToken cancellationToken = default(CancellationToken));
185+
186+
187+
/// <summary>
188+
/// Lists the databases on the server.
189+
/// </summary>
190+
/// <param name="session">The session.</param>
191+
/// <param name="cancellationToken">The cancellation token.</param>
192+
/// <returns>
193+
/// A Task whose result is a cursor.
194+
/// </returns>
195+
Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(
196+
IClientSessionHandle session,
197+
CancellationToken cancellationToken = default(CancellationToken));
110198

111199
/// <summary>
112200
/// Lists the databases on the server.
113201
/// </summary>
114202
/// <param name="session">The session.</param>
203+
/// <param name="options">The options.</param>
115204
/// <param name="cancellationToken">The cancellation token.</param>
116205
/// <returns>
117206
/// A Task whose result is a cursor.
118207
/// </returns>
119-
Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(IClientSessionHandle session, CancellationToken cancellationToken = default(CancellationToken));
208+
Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(
209+
IClientSessionHandle session,
210+
ListDatabasesOptions options,
211+
CancellationToken cancellationToken = default(CancellationToken));
120212

121213
/// <summary>
122-
/// Starts a client sesssion.
214+
/// Starts a client session.
123215
/// </summary>
124216
/// <param name="options">The session options.</param>
125217
/// <param name="cancellationToken">The cancellation token.</param>
@@ -129,7 +221,7 @@ public interface IMongoClient
129221
IClientSessionHandle StartSession(ClientSessionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
130222

131223
/// <summary>
132-
/// Starts a client sesssion.
224+
/// Starts a client session.
133225
/// </summary>
134226
/// <param name="options">The session options.</param>
135227
/// <param name="cancellationToken">The cancellation token.</param>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright 2017 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson;
17+
18+
namespace MongoDB.Driver
19+
{
20+
/// <summary>
21+
/// Options for a list databases operation.
22+
/// </summary>
23+
public sealed class ListDatabasesOptions
24+
{
25+
// fields
26+
private FilterDefinition<BsonDocument> _filter;
27+
private bool? _nameOnly;
28+
29+
// properties
30+
/// <summary>
31+
/// Gets or sets the filter.
32+
/// </summary>
33+
public FilterDefinition<BsonDocument> Filter
34+
{
35+
get { return _filter; }
36+
set { _filter = value; }
37+
}
38+
39+
/// <summary>
40+
/// Gets or sets the NameOnly flag.
41+
/// </summary>
42+
public bool? NameOnly
43+
{
44+
get { return _nameOnly; }
45+
set { _nameOnly = value; }
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)