Skip to content

Commit 0fe4b85

Browse files
committed
CSHARP-2274: Add ListCollectionNames to IMongoDatabase.
1 parent bbcda37 commit 0fe4b85

File tree

11 files changed

+403
-12
lines changed

11 files changed

+403
-12
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ListCollectionsOperation : IReadOperation<IAsyncCursor<BsonDocument
3636
private BsonDocument _filter;
3737
private readonly DatabaseNamespace _databaseNamespace;
3838
private readonly MessageEncoderSettings _messageEncoderSettings;
39+
private bool? _nameOnly;
3940

4041
// constructors
4142
/// <summary>
@@ -86,6 +87,18 @@ public MessageEncoderSettings MessageEncoderSettings
8687
get { return _messageEncoderSettings; }
8788
}
8889

90+
/// <summary>
91+
/// Gets or sets the name only option.
92+
/// </summary>
93+
/// <value>
94+
/// The name only option.
95+
/// </value>
96+
public bool? NameOnly
97+
{
98+
get { return _nameOnly; }
99+
set { _nameOnly = value; }
100+
}
101+
89102
// public methods
90103
/// <inheritdoc/>
91104
public IAsyncCursor<BsonDocument> Execute(IReadBinding binding, CancellationToken cancellationToken)
@@ -122,11 +135,18 @@ private IReadOperation<IAsyncCursor<BsonDocument>> CreateOperation(IChannel chan
122135
{
123136
if (Feature.ListCollectionsCommand.IsSupported(channel.ConnectionDescription.ServerVersion))
124137
{
125-
return new ListCollectionsUsingCommandOperation(_databaseNamespace, _messageEncoderSettings) { Filter = _filter };
138+
return new ListCollectionsUsingCommandOperation(_databaseNamespace, _messageEncoderSettings)
139+
{
140+
Filter = _filter,
141+
NameOnly = _nameOnly
142+
};
126143
}
127144
else
128145
{
129-
return new ListCollectionsUsingQueryOperation(_databaseNamespace, _messageEncoderSettings) { Filter = _filter };
146+
return new ListCollectionsUsingQueryOperation(_databaseNamespace, _messageEncoderSettings)
147+
{
148+
Filter = _filter
149+
};
130150
}
131151
}
132152
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ListCollectionsUsingCommandOperation : IReadOperation<IAsyncCursor<
3636
private BsonDocument _filter;
3737
private readonly DatabaseNamespace _databaseNamespace;
3838
private readonly MessageEncoderSettings _messageEncoderSettings;
39+
private bool? _nameOnly;
3940

4041
// constructors
4142
/// <summary>
@@ -86,6 +87,18 @@ public MessageEncoderSettings MessageEncoderSettings
8687
get { return _messageEncoderSettings; }
8788
}
8889

90+
/// <summary>
91+
/// Gets or sets the name only option.
92+
/// </summary>
93+
/// <value>
94+
/// The name only option.
95+
/// </value>
96+
public bool? NameOnly
97+
{
98+
get { return _nameOnly; }
99+
set { _nameOnly = value; }
100+
}
101+
89102
// public methods
90103
/// <inheritdoc/>
91104
public IAsyncCursor<BsonDocument> Execute(IReadBinding binding, CancellationToken cancellationToken)
@@ -121,7 +134,8 @@ private ReadCommandOperation<BsonDocument> CreateOperation()
121134
var command = new BsonDocument
122135
{
123136
{ "listCollections", 1 },
124-
{ "filter", _filter, _filter != null }
137+
{ "filter", _filter, _filter != null },
138+
{ "nameOnly", () => _nameOnly.Value, _nameOnly.HasValue }
125139
};
126140
return new ReadCommandOperation<BsonDocument>(_databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
127141
}

src/MongoDB.Driver/IMongoDatabase.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,49 @@ public interface IMongoDatabase
181181
IMongoCollection<TDocument> GetCollection<TDocument>(string name, MongoCollectionSettings settings = null);
182182

183183
/// <summary>
184-
/// Lists all the collections on the server.
184+
/// Lists the names of all the collections in the database.
185+
/// </summary>
186+
/// <param name="options">The options.</param>
187+
/// <param name="cancellationToken">The cancellation token.</param>
188+
/// <returns>A cursor.</returns>
189+
IAsyncCursor<string> ListCollectionNames(ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
190+
191+
/// <summary>
192+
/// Lists the names of all the collections in the database.
193+
/// </summary>
194+
/// <param name="session">The session.</param>
195+
/// <param name="options">The options.</param>
196+
/// <param name="cancellationToken">The cancellation token.</param>
197+
/// <returns>A cursor.</returns>
198+
IAsyncCursor<string> ListCollectionNames(IClientSessionHandle session, ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
199+
200+
/// <summary>
201+
/// Lists the names of all the collections in the database.
202+
/// </summary>
203+
/// <param name="options">The options.</param>
204+
/// <param name="cancellationToken">The cancellation token.</param>
205+
/// <returns>A Task whose result is a cursor.</returns>
206+
Task<IAsyncCursor<string>> ListCollectionNamesAsync(ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
207+
208+
/// <summary>
209+
/// Lists the names of all the collections in the database.
210+
/// </summary>
211+
/// <param name="session">The session.</param>
212+
/// <param name="options">The options.</param>
213+
/// <param name="cancellationToken">The cancellation token.</param>
214+
/// <returns>A Task whose result is a cursor.</returns>
215+
Task<IAsyncCursor<string>> ListCollectionNamesAsync(IClientSessionHandle session, ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
216+
217+
/// <summary>
218+
/// Lists all the collections in the database.
185219
/// </summary>
186220
/// <param name="options">The options.</param>
187221
/// <param name="cancellationToken">The cancellation token.</param>
188222
/// <returns>A cursor.</returns>
189223
IAsyncCursor<BsonDocument> ListCollections(ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
190224

191225
/// <summary>
192-
/// Lists all the collections on the server.
226+
/// Lists all the collections in the database.
193227
/// </summary>
194228
/// <param name="session">The session.</param>
195229
/// <param name="options">The options.</param>
@@ -200,15 +234,15 @@ public interface IMongoDatabase
200234
IAsyncCursor<BsonDocument> ListCollections(IClientSessionHandle session, ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
201235

202236
/// <summary>
203-
/// Lists all the collections on the server.
237+
/// Lists all the collections in the database.
204238
/// </summary>
205239
/// <param name="options">The options.</param>
206240
/// <param name="cancellationToken">The cancellation token.</param>
207241
/// <returns>A Task whose result is a cursor.</returns>
208242
Task<IAsyncCursor<BsonDocument>> ListCollectionsAsync(ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
209243

210244
/// <summary>
211-
/// Lists all the collections on the server.
245+
/// Lists all the collections in the database.
212246
/// </summary>
213247
/// <param name="session">The session.</param>
214248
/// <param name="options">The options.</param>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright 2018-present 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 collection names operation.
22+
/// </summary>
23+
public sealed class ListCollectionNamesOptions
24+
{
25+
// fields
26+
private FilterDefinition<BsonDocument> _filter;
27+
28+
// properties
29+
/// <summary>
30+
/// Gets or sets the filter.
31+
/// </summary>
32+
public FilterDefinition<BsonDocument> Filter
33+
{
34+
get { return _filter; }
35+
set { _filter = value; }
36+
}
37+
}
38+
}

src/MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
<Compile Include="Linq\Translators\QueryableTranslator.cs" />
291291
<Compile Include="Linq\Translators\PredicateTranslator.cs" />
292292
<Compile Include="ExpressionTranslationOptions.cs" />
293+
<Compile Include="ListCollectionNamesOptions.cs" />
293294
<Compile Include="ListDatabasesOptions.cs" />
294295
<Compile Include="MongoX509Identity.cs" />
295296
<Compile Include="OfTypeSerializer.cs" />

src/MongoDB.Driver/MongoDatabaseBase.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ public abstract class MongoDatabaseBase : IMongoDatabase
105105
/// <inheritdoc />
106106
public abstract IMongoCollection<TDocument> GetCollection<TDocument>(string name, MongoCollectionSettings settings = null);
107107

108+
/// <inheritdoc />
109+
public virtual IAsyncCursor<string> ListCollectionNames(ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
110+
{
111+
throw new NotImplementedException();
112+
}
113+
114+
/// <inheritdoc />
115+
public virtual IAsyncCursor<string> ListCollectionNames(IClientSessionHandle session, ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
116+
{
117+
throw new NotImplementedException();
118+
}
119+
120+
/// <inheritdoc />
121+
public virtual Task<IAsyncCursor<string>> ListCollectionNamesAsync(ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
122+
{
123+
throw new NotImplementedException();
124+
}
125+
126+
/// <inheritdoc />
127+
public virtual Task<IAsyncCursor<string>> ListCollectionNamesAsync(IClientSessionHandle session, ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
128+
{
129+
throw new NotImplementedException();
130+
}
131+
108132
/// <inheritdoc />
109133
public virtual IAsyncCursor<BsonDocument> ListCollections(ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
110134
{

src/MongoDB.Driver/MongoDatabaseImpl.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515

1616
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
1719
using System.Reflection;
1820
using System.Threading;
1921
using System.Threading.Tasks;
@@ -191,6 +193,34 @@ public override IMongoCollection<TDocument> GetCollection<TDocument>(string name
191193
return new MongoCollectionImpl<TDocument>(this, new CollectionNamespace(_databaseNamespace, name), settings, _cluster, _operationExecutor);
192194
}
193195

196+
public override IAsyncCursor<string> ListCollectionNames(ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
197+
{
198+
return UsingImplicitSession(session => ListCollectionNames(session, options, cancellationToken), cancellationToken);
199+
}
200+
201+
public override IAsyncCursor<string> ListCollectionNames(IClientSessionHandle session, ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
202+
{
203+
Ensure.IsNotNull(session, nameof(session));
204+
var operation = CreateListCollectionNamesOperation(options);
205+
var effectiveReadPreference = ReadPreferenceResolver.GetEffectiveReadPreference(session, null, ReadPreference.Primary);
206+
var cursor = ExecuteReadOperation(session, operation, effectiveReadPreference, cancellationToken);
207+
return new BatchTransformingAsyncCursor<BsonDocument, string>(cursor, ExtractCollectionNames);
208+
}
209+
210+
public override Task<IAsyncCursor<string>> ListCollectionNamesAsync(ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
211+
{
212+
return UsingImplicitSessionAsync(session => ListCollectionNamesAsync(session, options, cancellationToken), cancellationToken);
213+
}
214+
215+
public override async Task<IAsyncCursor<string>> ListCollectionNamesAsync(IClientSessionHandle session, ListCollectionNamesOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
216+
{
217+
Ensure.IsNotNull(session, nameof(session));
218+
var operation = CreateListCollectionNamesOperation(options);
219+
var effectiveReadPreference = ReadPreferenceResolver.GetEffectiveReadPreference(session, null, ReadPreference.Primary);
220+
var cursor = await ExecuteReadOperationAsync(session, operation, effectiveReadPreference, cancellationToken).ConfigureAwait(false);
221+
return new BatchTransformingAsyncCursor<BsonDocument, string>(cursor, ExtractCollectionNames);
222+
}
223+
194224
public override IAsyncCursor<BsonDocument> ListCollections(ListCollectionsOptions options, CancellationToken cancellationToken)
195225
{
196226
return UsingImplicitSession(session => ListCollections(session, options, cancellationToken), cancellationToken);
@@ -199,7 +229,6 @@ public override IAsyncCursor<BsonDocument> ListCollections(ListCollectionsOption
199229
public override IAsyncCursor<BsonDocument> ListCollections(IClientSessionHandle session, ListCollectionsOptions options, CancellationToken cancellationToken)
200230
{
201231
Ensure.IsNotNull(session, nameof(session));
202-
options = options ?? new ListCollectionsOptions();
203232
var operation = CreateListCollectionsOperation(options);
204233
var effectiveReadPreference = ReadPreferenceResolver.GetEffectiveReadPreference(session, null, ReadPreference.Primary);
205234
return ExecuteReadOperation(session, operation, effectiveReadPreference, cancellationToken);
@@ -213,7 +242,6 @@ public override Task<IAsyncCursor<BsonDocument>> ListCollectionsAsync(ListCollec
213242
public override Task<IAsyncCursor<BsonDocument>> ListCollectionsAsync(IClientSessionHandle session, ListCollectionsOptions options, CancellationToken cancellationToken)
214243
{
215244
Ensure.IsNotNull(session, nameof(session));
216-
options = options ?? new ListCollectionsOptions();
217245
var operation = CreateListCollectionsOperation(options);
218246
var effectiveReadPreference = ReadPreferenceResolver.GetEffectiveReadPreference(session, null, ReadPreference.Primary);
219247
return ExecuteReadOperationAsync(session, operation, effectiveReadPreference, cancellationToken);
@@ -396,12 +424,22 @@ private DropCollectionOperation CreateDropCollectionOperation(string name)
396424
};
397425
}
398426

427+
private ListCollectionsOperation CreateListCollectionNamesOperation(ListCollectionNamesOptions options)
428+
{
429+
var messageEncoderSettings = GetMessageEncoderSettings();
430+
return new ListCollectionsOperation(_databaseNamespace, messageEncoderSettings)
431+
{
432+
Filter = options?.Filter?.Render(_settings.SerializerRegistry.GetSerializer<BsonDocument>(), _settings.SerializerRegistry),
433+
NameOnly = true
434+
};
435+
}
436+
399437
private ListCollectionsOperation CreateListCollectionsOperation(ListCollectionsOptions options)
400438
{
401439
var messageEncoderSettings = GetMessageEncoderSettings();
402440
return new ListCollectionsOperation(_databaseNamespace, messageEncoderSettings)
403441
{
404-
Filter = options.Filter?.Render(_settings.SerializerRegistry.GetSerializer<BsonDocument>(), _settings.SerializerRegistry)
442+
Filter = options?.Filter?.Render(_settings.SerializerRegistry.GetSerializer<BsonDocument>(), _settings.SerializerRegistry)
405443
};
406444
}
407445

@@ -442,6 +480,11 @@ private ReadCommandOperation<TResult> CreateRunCommandOperation<TResult>(Command
442480
return new ReadCommandOperation<TResult>(_databaseNamespace, renderedCommand.Document, renderedCommand.ResultSerializer, messageEncoderSettings);
443481
}
444482

483+
private IEnumerable<string> ExtractCollectionNames(IEnumerable<BsonDocument> collections)
484+
{
485+
return collections.Select(collection => collection["name"].AsString);
486+
}
487+
445488
private T ExecuteReadOperation<T>(IClientSessionHandle session, IReadOperation<T> operation, ReadPreference readPreference, CancellationToken cancellationToken)
446489
{
447490
using (var binding = CreateReadBinding(session, readPreference))

0 commit comments

Comments
 (0)