Skip to content

Commit a029889

Browse files
authored
Merge pull request #2607 from coldays/add_modulo_opeator
Added missing modulo operator to BsonValue
2 parents 8abcf94 + 38ff333 commit a029889

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

LiteDB.Tests/Expressions/Expressions_Exec_Tests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ BsonValue S(string s, params BsonValue[] args)
104104
return BsonExpression.Create(s, args).ExecuteScalar(doc);
105105
}
106106

107+
// Arithmetic operators
108+
doc = new BsonDocument();
109+
// Int
110+
S("6 + 3").ExpectValue(9);
111+
S("6 * 3").ExpectValue(18);
112+
S("6 % 3").ExpectValue(0);
113+
S("6 / 3").ExpectValue(2);
114+
// Double
115+
S("6.0 + 3.0").ExpectValue(9);
116+
S("6.0 * 3.0").ExpectValue(18);
117+
S("6.0 % 3.0").ExpectValue(0);
118+
S("6.0 / 3.0").ExpectValue(2);
119+
107120
// Operators order
108121
doc = J("{ a: 1, b: 2, c: 3 }");
109122

LiteDB/Document/BsonValue.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ public static implicit operator BsonValue(DateTime value)
500500
return left.AsDouble / right.AsDouble;
501501
}
502502

503+
// %
504+
public static BsonValue operator %(BsonValue left, BsonValue right)
505+
{
506+
if (!left.IsNumber || !right.IsNumber) return BsonValue.Null;
507+
if (left.IsInt32 && right.IsInt32) return left.AsInt32 % right.AsInt32;
508+
if (left.IsInt64 && right.IsInt64) return left.AsInt64 % right.AsInt64;
509+
if (left.IsDecimal && right.IsDecimal) return left.AsDecimal % right.AsDecimal;
510+
511+
return left.AsDouble % right.AsDouble;
512+
}
513+
503514
public override string ToString()
504515
{
505516
return JsonSerializer.Serialize(this);

0 commit comments

Comments
 (0)