Permalink
Browse files

Added support for Distinct<TValue>.

  • Loading branch information...
1 parent 8a86707 commit 3df21a6b24bb7126094483f5bbc686da71c3876e rstam committed Mar 25, 2013
Showing with 83 additions and 0 deletions.
  1. +48 −0 MongoDB.Driver/MongoCollection.cs
  2. +35 −0 MongoDB.DriverUnitTests/MongoCollectionTests.cs
@@ -257,6 +257,54 @@ public virtual IEnumerable<BsonValue> Distinct(string key, IMongoQuery query)
}
/// <summary>
+ /// Returns the distinct values for a given field.
+ /// </summary>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ /// <param name="key">The key of the field.</param>
+ /// <returns>The distint values of the field.</returns>
+ public virtual IEnumerable<TValue> Distinct<TValue>(string key)
+ {
+ return Distinct<TValue>(key, Query.Null);
+ }
+
+ /// <summary>
+ /// Returns the distinct values for a given field for documents that match a query.
+ /// </summary>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ /// <param name="key">The key of the field.</param>
+ /// <param name="query">The query (usually a QueryDocument or constructed using the Query builder).</param>
+ /// <returns>The distint values of the field.</returns>
+ public virtual IEnumerable<TValue> Distinct<TValue>(string key, IMongoQuery query)
+ {
+ var command = new CommandDocument
+ {
+ { "distinct", _name },
+ { "key", key },
+ { "query", BsonDocumentWrapper.Create(query), query != null } // query is optional
+ };
+ var result = RunCommand(command);
+
+ using (var bsonReader = BsonReader.Create(result.Response))
+ {
+ var serializer = BsonSerializer.LookupSerializer(typeof(TValue));
+ bsonReader.ReadStartDocument();
+ if (bsonReader.FindElement("values"))
+ {
+ bsonReader.ReadStartArray();
+ while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
+ {
+ yield return (TValue)serializer.Deserialize(bsonReader, typeof(TValue), null);
+ }
+ bsonReader.ReadEndArray();
+ }
+ else
+ {
+ throw new FormatException("Command Response is missing the values element.");
+ }
+ }
+ }
+
+ /// <summary>
/// Drops this collection.
/// </summary>
/// <returns>A CommandResult.</returns>
@@ -241,6 +241,23 @@ public void TestDistinct()
}
[Test]
+ public void TestDistinct_Typed()
+ {
+ _collection.RemoveAll();
+ _collection.DropAllIndexes();
+ _collection.Insert(new BsonDocument("x", 1));
+ _collection.Insert(new BsonDocument("x", 2));
+ _collection.Insert(new BsonDocument("x", 3));
+ _collection.Insert(new BsonDocument("x", 3));
+ var values = new HashSet<int>(_collection.Distinct<int>("x"));
+ Assert.AreEqual(3, values.Count);
+ Assert.AreEqual(true, values.Contains(1));
+ Assert.AreEqual(true, values.Contains(2));
+ Assert.AreEqual(true, values.Contains(3));
+ Assert.AreEqual(false, values.Contains(4));
+ }
+
+ [Test]
public void TestDistinctWithQuery()
{
_collection.RemoveAll();
@@ -259,6 +276,24 @@ public void TestDistinctWithQuery()
}
[Test]
+ public void TestDistinctWithQuery_Typed()
+ {
+ _collection.RemoveAll();
+ _collection.DropAllIndexes();
+ _collection.Insert(new BsonDocument("x", 1));
+ _collection.Insert(new BsonDocument("x", 2));
+ _collection.Insert(new BsonDocument("x", 3));
+ _collection.Insert(new BsonDocument("x", 3));
+ var query = Query.LTE("x", 2);
+ var values = new HashSet<int>(_collection.Distinct<int>("x", query));
+ Assert.AreEqual(2, values.Count);
+ Assert.AreEqual(true, values.Contains(1));
+ Assert.AreEqual(true, values.Contains(2));
+ Assert.AreEqual(false, values.Contains(3));
+ Assert.AreEqual(false, values.Contains(4));
+ }
+
+ [Test]
public void TestDropAllIndexes()
{
_collection.DropAllIndexes();

0 comments on commit 3df21a6

Please sign in to comment.