litedb-org/LiteDB · error · InvalidOperationException
Cannot access non-array type value on {this.RawValue}
Error message
Cannot access non-array type value on {this.RawValue} What it means
Thrown by the virtual int-indexer on the base BsonValue class when an element access (value[index]) is attempted on a BsonValue that is not a BsonArray. Only BsonArray overrides this indexer; all other types inherit the throwing base implementation. The message includes the current RawValue for diagnostics.
Source
Thrown at LiteDB/Document/BsonValue.cs:212
#endregion
#region Index "this" property
/// <summary>
/// Get/Set a field for document. Fields are case sensitive - Works only when value are document
/// </summary>
public virtual BsonValue this[string name]
{
get => throw new InvalidOperationException("Cannot access non-document type value on " + this.RawValue);
set => throw new InvalidOperationException("Cannot access non-document type value on " + this.RawValue);
}
/// <summary>
/// Get/Set value in array position. Works only when value are array
/// </summary>
public virtual BsonValue this[int index]
{
get => throw new InvalidOperationException("Cannot access non-array type value on " + this.RawValue);
set => throw new InvalidOperationException("Cannot access non-array type value on " + this.RawValue);
}
#endregion
#region Convert types
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonArray AsArray => this as BsonArray;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonDocument AsDocument => this as BsonDocument;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Byte[] AsBinary => this.RawValue as Byte[];
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public bool AsBoolean => (bool)this.RawValue;View on GitHub (pinned to f906a5f850)
Solutions
- Check `value.IsArray` (or `value.AsArray != null`) before indexing.
- Use `value.AsArray` with a null-check and default to an empty result.
- Normalize the stored data so the field is consistently an array.
Example fix
// before var first = items[0]; // after var first = items.IsArray && items.AsArray.Count > 0 ? items[0] : BsonValue.Null;
Defensive patterns
Strategy: type-guard
Validate before calling
if (!value.IsArray) throw new InvalidOperationException($"Expected array, got {value.Type}");
var item = value[0]; Type guard
static bool IsBsonArray(BsonValue v) => v != null && v.IsArray;
Prevention
- Always check value.IsArray before using the int indexer.
- Use value.AsArray and check for null/count before indexing.
- Normalize stored data so array fields are consistently arrays.
When it happens
Trigger: Accessing `someBsonValue[0]` or `value[i]` where the value is a document, string, number, null, or any non-array type. Treating a scalar result as a list and indexing into it.
Common situations: A query field that was expected to be an array is stored as a single scalar or document. Reading a field that is sometimes null. Mixed data where some documents have arrays and others have scalars for the same field.
Related errors
- Cannot access non-document type value on {this.RawValue}
- The current collection implementation does not support vecto
- Value cannot be null. (Parameter 'doc')
- Value cannot be null. (Parameter 'buffer')
- Value cannot be null. (Parameter 'array')
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/60e7caec96ff26e3.
Report an issue: GitHub.