Skip to content

Commit

Permalink
Merge pull request #209 from TurnerSoftware/multiple-indexes-on-property
Browse files Browse the repository at this point in the history
Add support for multiple indexes on a property
  • Loading branch information
Turnerj authored Dec 17, 2020
2 parents 286bef6 + 0d2b14c commit fa9438d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MongoFramework/Attributes/IndexAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MongoFramework.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class IndexAttribute : Attribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public class TenantUniqueConstraintModel : IHaveTenantId
public string UniqueIndex { get; set; }
}

public class MultipleIndexesOnSinglePropertyModel
{
[Index("MyMainIndex", IndexSortOrder.Ascending)]
[Index("MyCompoundIndex", IndexSortOrder.Ascending, IndexPriority = 1)]
public string MyCustomField { get; set; }

[Index("MyCompoundIndex", IndexSortOrder.Descending, IndexPriority = 2)]
public string OtherField { get; set; }
}

[TestMethod]
public void IndexNaming()
Expand Down Expand Up @@ -204,6 +213,16 @@ public void AppliesTenantConstraint()
Assert.AreEqual("{ \"TenantId\" : 1, \"UniqueIndex\" : 1 }", indexBsonDocument);
}

[TestMethod]
public void MultipleIndexesOnSingleProperty()
{
var indexModel = IndexModelBuilder<MultipleIndexesOnSinglePropertyModel>.BuildModel();

Assert.AreEqual(2, indexModel.Count());

var results = indexModel.Select(i => i.Keys.Render(null, null));
Assert.IsTrue(results.Any(e => e.Contains("MyCustomField") && e.ElementCount == 1));
Assert.IsTrue(results.Any(e => e.Contains("MyCustomField") && e.Contains("OtherField") && e.ElementCount == 2));
}
}
}

0 comments on commit fa9438d

Please sign in to comment.