diff --git a/LiteDB.Tests/Engine/Index_Tests.cs b/LiteDB.Tests/Engine/Index_Tests.cs index 549f63194..bdd468c17 100644 --- a/LiteDB.Tests/Engine/Index_Tests.cs +++ b/LiteDB.Tests/Engine/Index_Tests.cs @@ -15,8 +15,8 @@ public void Index_With_No_Name() var users = db.GetCollection("users"); var indexes = db.GetCollection("$indexes"); - users.Insert(new BsonDocument {["name"] = new BsonDocument {["first"] = "John", ["last"] = "Doe"}}); - users.Insert(new BsonDocument {["name"] = new BsonDocument {["first"] = "Marco", ["last"] = "Pollo"}}); + users.Insert(new BsonDocument { ["name"] = new BsonDocument { ["first"] = "John", ["last"] = "Doe" } }); + users.Insert(new BsonDocument { ["name"] = new BsonDocument { ["first"] = "Marco", ["last"] = "Pollo" } }); // no index name defined users.EnsureIndex("name.last"); @@ -36,11 +36,11 @@ public void Index_Order() var col = db.GetCollection("col"); var indexes = db.GetCollection("$indexes"); - col.Insert(new BsonDocument {{"text", "D"}}); - col.Insert(new BsonDocument {{"text", "A"}}); - col.Insert(new BsonDocument {{"text", "E"}}); - col.Insert(new BsonDocument {{"text", "C"}}); - col.Insert(new BsonDocument {{"text", "B"}}); + col.Insert(new BsonDocument { { "text", "D" } }); + col.Insert(new BsonDocument { { "text", "A" } }); + col.Insert(new BsonDocument { { "text", "E" } }); + col.Insert(new BsonDocument { { "text", "C" } }); + col.Insert(new BsonDocument { { "text", "B" } }); col.EnsureIndex("text"); @@ -139,5 +139,63 @@ public void EnsureIndex_Invalid_Arguments() Assert.Equal("expression", exn.ParamName); } } + + [Fact] + public void MultiKey_Index_Test() + { + using var db = new LiteDatabase("filename=:memory:"); + var col = db.GetCollection("customers", BsonAutoId.Int32); + col.EnsureIndex("$.Phones[*].Type"); + + var doc1 = new BsonDocument + { + ["Name"] = "John Doe", + ["Phones"] = new BsonArray + ( + new BsonDocument + { + ["Type"] = "Mobile", + ["Number"] = "9876-5432" + }, + new BsonDocument + { + ["Type"] = "Fixed", + ["Number"] = "3333-3333" + } + ) + }; + + var doc2 = new BsonDocument + { + ["Name"] = "Jane Doe", + ["Phones"] = new BsonArray + ( + new BsonDocument + { + ["Type"] = "Fixed", + ["Number"] = "3000-0000" + } + ) + }; + + col.Insert(doc1); + col.Insert(doc2); + + var query1 = "select $ from customers where $.Phones[*].Type any = 'Mobile'"; + var query2 = "select $ from customers where $.Phones[*].Type any = 'Fixed'"; + + var explain1 = db.Execute("explain " + query1).First(); + Assert.True(!explain1["index"]["mode"].AsString.Contains("_id")); + + var explain2 = db.Execute("explain " + query2).First(); + Assert.True(!explain2["index"]["mode"].AsString.Contains("_id")); + + + var result1 = db.Execute(query1).ToArray(); + Assert.True(result1.Length == 1); + + var result2 = db.Execute(query2).ToArray(); + Assert.True(result2.Length == 2); + } } } \ No newline at end of file diff --git a/LiteDB/Document/BsonArray.cs b/LiteDB/Document/BsonArray.cs index 78688a883..c8d3b6e1d 100644 --- a/LiteDB/Document/BsonArray.cs +++ b/LiteDB/Document/BsonArray.cs @@ -21,7 +21,7 @@ public BsonArray(List array) this.AddRange(array); } - public BsonArray(BsonValue[] array) + public BsonArray(params BsonValue[] array) : this() { if (array == null) throw new ArgumentNullException(nameof(array));