{"record":{"id":"60e7caec96ff26e3","repo":"litedb-org/LiteDB","slug":"cannot-access-non-array-type-value-on-this-rawval","errorCode":null,"errorMessage":"Cannot access non-array type value on {this.RawValue}","messagePattern":"Cannot access non-array type value on (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"LiteDB/Document/BsonValue.cs","lineNumber":212,"sourceCode":"        #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;\n\n        [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n        public BsonDocument AsDocument => this as BsonDocument;\n\n        [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n        public Byte[] AsBinary => this.RawValue as Byte[];\n\n        [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n        public bool AsBoolean => (bool)this.RawValue;","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Document/BsonValue.cs#L194-L230","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar first = items[0];\n// after\nvar first = items.IsArray && items.AsArray.Count > 0 ? items[0] : BsonValue.Null;","handlingStrategy":"type-guard","validationCode":"if (!value.IsArray) throw new InvalidOperationException($\"Expected array, got {value.Type}\");\nvar item = value[0];","typeGuard":"static bool IsBsonArray(BsonValue v) => v != null && v.IsArray;","tryCatchPattern":null,"preventionTips":["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."],"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"}