Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple indexes on a property #209

Merged
merged 1 commit into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
}