Skip to content

Commit 3df21a6

Browse files
author
rstam
committed
Added support for Distinct<TValue>.
1 parent 8a86707 commit 3df21a6

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

MongoDB.Driver/MongoCollection.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,54 @@ public virtual IEnumerable<BsonValue> Distinct(string key, IMongoQuery query)
256256
return result.Response["values"].AsBsonArray;
257257
}
258258

259+
/// <summary>
260+
/// Returns the distinct values for a given field.
261+
/// </summary>
262+
/// <typeparam name="TValue">The type of the value.</typeparam>
263+
/// <param name="key">The key of the field.</param>
264+
/// <returns>The distint values of the field.</returns>
265+
public virtual IEnumerable<TValue> Distinct<TValue>(string key)
266+
{
267+
return Distinct<TValue>(key, Query.Null);
268+
}
269+
270+
/// <summary>
271+
/// Returns the distinct values for a given field for documents that match a query.
272+
/// </summary>
273+
/// <typeparam name="TValue">The type of the value.</typeparam>
274+
/// <param name="key">The key of the field.</param>
275+
/// <param name="query">The query (usually a QueryDocument or constructed using the Query builder).</param>
276+
/// <returns>The distint values of the field.</returns>
277+
public virtual IEnumerable<TValue> Distinct<TValue>(string key, IMongoQuery query)
278+
{
279+
var command = new CommandDocument
280+
{
281+
{ "distinct", _name },
282+
{ "key", key },
283+
{ "query", BsonDocumentWrapper.Create(query), query != null } // query is optional
284+
};
285+
var result = RunCommand(command);
286+
287+
using (var bsonReader = BsonReader.Create(result.Response))
288+
{
289+
var serializer = BsonSerializer.LookupSerializer(typeof(TValue));
290+
bsonReader.ReadStartDocument();
291+
if (bsonReader.FindElement("values"))
292+
{
293+
bsonReader.ReadStartArray();
294+
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
295+
{
296+
yield return (TValue)serializer.Deserialize(bsonReader, typeof(TValue), null);
297+
}
298+
bsonReader.ReadEndArray();
299+
}
300+
else
301+
{
302+
throw new FormatException("Command Response is missing the values element.");
303+
}
304+
}
305+
}
306+
259307
/// <summary>
260308
/// Drops this collection.
261309
/// </summary>

MongoDB.DriverUnitTests/MongoCollectionTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,23 @@ public void TestDistinct()
240240
Assert.AreEqual(false, values.Contains(4));
241241
}
242242

243+
[Test]
244+
public void TestDistinct_Typed()
245+
{
246+
_collection.RemoveAll();
247+
_collection.DropAllIndexes();
248+
_collection.Insert(new BsonDocument("x", 1));
249+
_collection.Insert(new BsonDocument("x", 2));
250+
_collection.Insert(new BsonDocument("x", 3));
251+
_collection.Insert(new BsonDocument("x", 3));
252+
var values = new HashSet<int>(_collection.Distinct<int>("x"));
253+
Assert.AreEqual(3, values.Count);
254+
Assert.AreEqual(true, values.Contains(1));
255+
Assert.AreEqual(true, values.Contains(2));
256+
Assert.AreEqual(true, values.Contains(3));
257+
Assert.AreEqual(false, values.Contains(4));
258+
}
259+
243260
[Test]
244261
public void TestDistinctWithQuery()
245262
{
@@ -258,6 +275,24 @@ public void TestDistinctWithQuery()
258275
Assert.AreEqual(false, values.Contains(4));
259276
}
260277

278+
[Test]
279+
public void TestDistinctWithQuery_Typed()
280+
{
281+
_collection.RemoveAll();
282+
_collection.DropAllIndexes();
283+
_collection.Insert(new BsonDocument("x", 1));
284+
_collection.Insert(new BsonDocument("x", 2));
285+
_collection.Insert(new BsonDocument("x", 3));
286+
_collection.Insert(new BsonDocument("x", 3));
287+
var query = Query.LTE("x", 2);
288+
var values = new HashSet<int>(_collection.Distinct<int>("x", query));
289+
Assert.AreEqual(2, values.Count);
290+
Assert.AreEqual(true, values.Contains(1));
291+
Assert.AreEqual(true, values.Contains(2));
292+
Assert.AreEqual(false, values.Contains(3));
293+
Assert.AreEqual(false, values.Contains(4));
294+
}
295+
261296
[Test]
262297
public void TestDropAllIndexes()
263298
{

0 commit comments

Comments
 (0)