{"record":{"id":"d7f5611154b31a1d","repo":"litedb-org/LiteDB","slug":"cannot-access-non-document-type-value-on-this-raw","errorCode":null,"errorMessage":"Cannot access non-document type value on {this.RawValue}","messagePattern":"Cannot access non-document type value on (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"LiteDB/Document/BsonValue.cs","lineNumber":203,"sourceCode":"                    this.RawValue = list;\n                }\n                else\n                {\n                    throw new InvalidCastException(\"Value is not a valid BSON data type - Use Mapper.ToDocument for more complex types converts\");\n                }\n            }\n        }\n\n        #endregion\n\n        #region Index \"this\" property\n\n        /// <summary>\n        /// Get/Set a field for document. Fields are case sensitive - Works only when value are document\n        /// </summary>\n        public virtual BsonValue this[string name]\n        {\n            get => throw new InvalidOperationException(\"Cannot access non-document type value on \" + this.RawValue);\n            set => throw new InvalidOperationException(\"Cannot access non-document type value on \" + this.RawValue);\n        }\n\n        /// <summary>\n        /// Get/Set value in array position. Works only when value are array\n        /// </summary>\n        public virtual BsonValue this[int index]\n        {\n            get => throw new InvalidOperationException(\"Cannot access non-array type value on \" + this.RawValue);\n            set => throw new InvalidOperationException(\"Cannot access non-array type value on \" + this.RawValue);\n        }\n\n        #endregion\n\n        #region Convert types\n\n        [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n        public BsonArray AsArray => this as BsonArray;","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Document/BsonValue.cs#L185-L221","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check `value.IsDocument` (or `value.AsDocument != null`) before using the string indexer.","Use `value.AsDocument` with a null-check, which safely returns null instead of throwing for non-documents.","Validate the upstream query/mapping to ensure the value is actually a document."],"exampleFix":"// before\nvar name = result[\"name\"];\n// after\nvar name = result.IsDocument ? result[\"name\"] : BsonValue.Null;","handlingStrategy":"type-guard","validationCode":"if (!value.IsDocument) throw new InvalidOperationException($\"Expected document, got {value.Type}\");\nvar field = value[\"name\"];","typeGuard":"static bool IsBsonDocument(BsonValue v) => v != null && v.IsDocument;","tryCatchPattern":null,"preventionTips":["Always check value.IsDocument before using the string indexer.","Use value.AsDocument (returns null safely) instead of direct casting.","Validate query result shapes before indexing into them."],"tags":["bson","type-access","indexer","invalid-operation"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}