litedb-org/LiteDB · error · InvalidOperationException

Cannot access non-document type value on {this.RawValue}

Error message

Cannot access non-document type value on {this.RawValue}

What it means

Thrown by the virtual string-indexer on the base BsonValue class when a field access (doc["fieldName"]) is attempted on a BsonValue that is not a BsonDocument. Only BsonDocument overrides this indexer to return actual field values; all other BSON types inherit the throwing base implementation. The message includes the current RawValue for diagnostics.

Source

Thrown at LiteDB/Document/BsonValue.cs:203

                    this.RawValue = list;
                }
                else
                {
                    throw new InvalidCastException("Value is not a valid BSON data type - Use Mapper.ToDocument for more complex types converts");
                }
            }
        }

        #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;

View on GitHub (pinned to f906a5f850)

Solutions

  1. Check `value.IsDocument` (or `value.AsDocument != null`) before using the string indexer.
  2. Use `value.AsDocument` with a null-check, which safely returns null instead of throwing for non-documents.
  3. Validate the upstream query/mapping to ensure the value is actually a document.

Example fix

// before
var name = result["name"];
// after
var name = result.IsDocument ? result["name"] : BsonValue.Null;
Defensive patterns

Strategy: type-guard

Validate before calling

if (!value.IsDocument) throw new InvalidOperationException($"Expected document, got {value.Type}");
var field = value["name"];

Type guard

static bool IsBsonDocument(BsonValue v) => v != null && v.IsDocument;

Prevention

When it happens

Trigger: Accessing `someBsonValue["key"]` where the value is an array, string, number, null, or any non-document type. Iterating over mixed-type results and indexing into each without a type check.

Common situations: A query or aggregation returns a non-document value (e.g., a scalar or array) and code unconditionally indexes into it as if it were a document. Deserialized data with unexpected shape. Accessing a field on a BsonValue.Null result.

Related errors


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