litedb-org/LiteDB · error · ArgumentException
At least two Query should be passed
Error message
At least two Query should be passed
What it means
Query.And(params BsonExpression[] queries) folds many filters with AND. At Query.cs:206 it throws ArgumentException("At least two Query should be passed") when queries is null or has fewer than two elements, because AND is a binary operation that needs at least two operands. Passing a single filter or none is a usage error, not a null-element problem. This is an ArgumentException, not ArgumentNullException.
Source
Thrown at LiteDB/Client/Structures/Query.cs:206
public static QueryAny Any() => new QueryAny();
/// <summary>
/// Returns document that exists in BOTH queries results. If both queries has indexes, left query has index preference (other side will be run in full scan)
/// </summary>
public static BsonExpression And(BsonExpression left, BsonExpression right)
{
if (left == null) throw new ArgumentNullException(nameof(left));
if (right == null) throw new ArgumentNullException(nameof(right));
return $"({left.Source} AND {right.Source})";
}
/// <summary>
/// Returns document that exists in ALL queries results.
/// </summary>
public static BsonExpression And(params BsonExpression[] queries)
{
if (queries == null || queries.Length < 2) throw new ArgumentException("At least two Query should be passed");
var left = queries[0];
for (int i = 1; i < queries.Length; i++)
{
left = And(left, queries[i]);
}
return left;
}
/// <summary>
/// Returns documents that exists in ANY queries results (Union).
/// </summary>
public static BsonExpression Or(BsonExpression left, BsonExpression right)
{
if (left == null) throw new ArgumentNullException(nameof(left));
if (right == null) throw new ArgumentNullException(nameof(right));View on GitHub (pinned to f906a5f850)
Solutions
- Guard the collection: if fewer than 2 filters, return the single one (or Query.All) instead of calling And.
- Use the binary Query.And(left, right) when you know there are exactly two.
- Ensure at least two non-null elements are passed.
Example fix
// before
var q = Query.And(filters.ToArray()); // filters may have 0 or 1 element
// after
var q = filters.Count switch
{
0 => Query.All(),
1 => filters[0],
_ => Query.And(filters.ToArray())
}; Defensive patterns
Strategy: validation
Validate before calling
var q = filters.Count switch
{
0 => Query.All(),
1 => filters[0],
_ => Query.And(filters.ToArray())
}; Type guard
static bool CanFold(IReadOnlyList<BsonExpression> q) =>
q is not null && q.Count >= 2 && q.All(x => x is not null); Prevention
- Check the filter count before folding with the params overload.
- Use the binary And(left, right) when exactly two operands are known.
- Ensure no null elements sneak into the array (they throw at the binary overload).
When it happens
Trigger: Calling Query.And(), Query.And(single), Query.And(arr) where arr.Length < 2, or Query.And(null). A null array also triggers it (the check uses ||).
Common situations: Dynamically collecting filters from a request where only one (or zero) was provided; passing a list whose Count was not checked before folding.
Related errors
- Value cannot be null. (Parameter 'left')
- field
- Value cannot be null. (Parameter 'field')
- Value cannot be null. (Parameter 'arrayField')
- Multiple OrderBy calls are not supported. Use ThenBy for add
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/3bdf7243512576c4.
Report an issue: GitHub.