litedb-org/LiteDB · error · ArgumentNullException

Value cannot be null. (Parameter 'arrayField')

Error message

Value cannot be null. (Parameter 'arrayField')

What it means

QueryAny.EQ(string arrayField, BsonValue value) builds '$arrayField ANY = $value', matching documents where at least one element of the array field equals value. At QueryAny.cs:16 it throws ArgumentNullException(nameof(arrayField)) when arrayField is null/empty/whitespace, because the ANY operator needs a concrete array path. This is an instance method on the object returned by Query.Any().

Source

Thrown at LiteDB/Client/Structures/QueryAny.cs:16

using LiteDB.Engine;
using System;
using System.Collections.Generic;
using System.Linq;
using static LiteDB.Constants;

namespace LiteDB
{
    public class QueryAny
    {
        /// <summary>
        /// Returns all documents for which at least one value in arrayFields is equal to value
        /// </summary>
        public BsonExpression EQ(string arrayField, BsonValue value)
        {
            if (arrayField.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(arrayField));

            return BsonExpression.Create($"{arrayField} ANY = {value ?? BsonValue.Null}");
        }

        /// <summary>
        /// Returns all documents for which at least one value in arrayFields are less tha to value (&lt;)
        /// </summary>
        public BsonExpression LT(string arrayField, BsonValue value)
        {
            if (arrayField.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(arrayField));

            return BsonExpression.Create($"{arrayField} ANY < {value ?? BsonValue.Null}");
        }

        /// <summary>
        /// Returns all documents for which at least one value in arrayFields are less than or equals value (&lt;=)
        /// </summary>
        public BsonExpression LTE(string arrayField, BsonValue value)

View on GitHub (pinned to f906a5f850)

Solutions

  1. Pass the concrete array field: Query.Any().EQ("tags", tag).
  2. Validate the array field name before calling.
  3. Confirm the stored property actually holds a BsonArray so ANY is meaningful.

Example fix

// before
var q = Query.Any().EQ(arrField, tag); // arrField may be null

// after
if (string.IsNullOrWhiteSpace(arrField))
    throw new ArgumentException("Array field required.", nameof(arrField));
var q = Query.Any().EQ(arrField, tag);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(arrayField))
    throw new ArgumentException("Array field required.", nameof(arrayField));
var q = Query.Any().EQ(arrayField, value);

Type guard

static bool IsValidArrayField(string f) => !string.IsNullOrWhiteSpace(f);

Prevention

When it happens

Trigger: Calling Query.Any().EQ(null, v), Query.Any().EQ("", v), or Query.Any().EQ(" ", v). Happens when the array field name is read from a mapping that returned empty.

Common situations: Querying a tags/categories array where the field name was parameterized and left blank; forgetting the field is an array path and passing a scalar property name that resolved to empty.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/7dfb7a9643b91ac5. Report an issue: GitHub.