Skip to content
8 changes: 6 additions & 2 deletions LiteDB.Tests/Database/MultiKey_Mapper_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ public void MultiKey_Mapper()
col.Count(Query.Any().EQ("Keys", 2)).Should().Be(2);
col.Count(x => x.Keys.Contains(2)).Should().Be(2);

col.Count(Query.Any().StartsWith("Customers[*].Name", "Ana")).Should().Be(2);
col.Count(x => x.Customers.Select(z => z.Name).Any(z => z.StartsWith("Ana"))).Should().Be(2);
col.Count(Query.Any().StartsWith("Customers[*].Name", "An")).Should().Be(2);
col.Count(Query.Any().EndsWith("Customers[*].Name", "na")).Should().Be(2);
col.Count(Query.Any().Contains("Customers[*].Name", "Ana")).Should().Be(2);
col.Count(x => x.Customers.Select(z => z.Name).Any(z => z.StartsWith("An"))).Should().Be(2);
col.Count(x => x.Customers.Select(z => z.Name).Any(z => z.EndsWith("na"))).Should().Be(2);
col.Count(x => x.Customers.Select(z => z.Name).Any(z => z.Contains("Ana"))).Should().Be(2);

col.Count(Query.Any().StartsWith("Customers[*].Name", "D")).Should().Be(1);
col.Count(x => x.Customers.Select(z => z.Name).Any(z => z.StartsWith("D"))).Should().Be(1);
Expand Down
42 changes: 41 additions & 1 deletion LiteDB.Tests/Query/QueryApi_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FluentAssertions;
using FluentAssertions;
using System;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -32,5 +32,45 @@ public void Query_And_Same_Field()

AssertEx.ArrayEqual(r0, r1, true);
}

[Fact]
public void Query_StartsWith()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

var r0 = local.Where(x => x.Name.StartsWith("Jo")).ToArray();

var r1 = collection.Find(Query.StartsWith("Name", "Jo")).ToArray();

AssertEx.ArrayEqual(r0, r1, true);
}

[Fact]
public void Query_EndsWith()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

var r0 = local.Where(x => x.Name.EndsWith("er")).ToArray();

var r1 = collection.Find(Query.EndsWith("Name", "er")).ToArray();

AssertEx.ArrayEqual(r0, r1, true);
}

[Fact]
public void Query_Contains()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

// Use uppercase pattern to avoid case-sensitivity differences between LINQ and LiteDB LIKE
var r0 = local.Where(x => x.Name.Contains("John")).ToArray();

var r1 = collection.Find(Query.Contains("Name", "John")).ToArray();

AssertEx.ArrayEqual(r0, r1, true);
}
}
}
18 changes: 15 additions & 3 deletions LiteDB/Client/Structures/Query.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LiteDB.Engine;
using LiteDB.Engine;

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -115,8 +115,20 @@ public static BsonExpression StartsWith(string field, string value)
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));
if (value.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(value));

return BsonExpression.Create($"{field} LIKE {(new BsonValue(value + "%"))}");
return BsonExpression.Create($"{field} LIKE {new BsonValue(value + "%")}");
}

/// <summary>
/// Returns all documents that ends with value (LIKE)
/// </summary>
public static BsonExpression EndsWith(string field, string value)
Comment thread
rube200 marked this conversation as resolved.
{
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));
if (value.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(value));

return BsonExpression.Create($"{field} LIKE {new BsonValue("%" + value)}");
}


/// <summary>
/// Returns all documents that contains value (CONTAINS) - string Contains
Expand All @@ -126,7 +138,7 @@ public static BsonExpression Contains(string field, string value)
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));
if (value.IsNullOrEmpty()) throw new ArgumentNullException(nameof(value));

return BsonExpression.Create($"{field} LIKE {(new BsonValue("%" + value + "%"))}");
return BsonExpression.Create($"{field} LIKE {new BsonValue("%" + value + "%")}");
}

/// <summary>
Expand Down
24 changes: 23 additions & 1 deletion LiteDB/Client/Structures/QueryAny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,31 @@ public BsonExpression StartsWith(string arrayField, string value)
if (arrayField.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(arrayField));
if (value.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(value));

return BsonExpression.Create($"{arrayField} ANY LIKE {(new BsonValue(value + "%"))}");
return BsonExpression.Create($"{arrayField} ANY LIKE {new BsonValue(value + "%")}");
}

/// <summary>
/// Returns all documents for which at least one value in arrayFields ends with value (LIKE)
/// </summary>
public BsonExpression EndsWith(string arrayField, string value)
{
if (arrayField.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(arrayField));
if (value.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(value));

return BsonExpression.Create($"{arrayField} ANY LIKE {new BsonValue("%" + value)}");
}

/// <summary>
/// Returns all documents for which at least one value in arrayFields contains the value (CONTAINS)
/// </summary>
public BsonExpression Contains(string arrayField, string value)
{
if (arrayField.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(arrayField));
if (value.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(value));

return BsonExpression.Create($"{arrayField} ANY LIKE {new BsonValue("%" + value + "%")}");
}

/// <summary>
/// Returns all documents for which at least one value in arrayFields are not equals to value (not equals)
/// </summary>
Expand Down
Loading