// public indexers
        /// <summary>
        /// Gets or sets a value by position.
        /// </summary>
        /// <param name="index">The position.</param>
        /// <returns>The value.</returns>
        public override BsonValue this[int index]
        {
            get
            {
                if (index < 0)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                ThrowIfDisposed();

                using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
                using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
                {
                    bsonReader.ReadStartDocument();
                    var i = 0;
                    while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                    {
                        bsonReader.SkipName();
                        if (i == index)
                        {
                            var context = BsonDeserializationContext.CreateRoot(bsonReader);
                            return DeserializeBsonValue(context);
                        }

                        bsonReader.SkipValue();
                        i++;
                    }
                    bsonReader.ReadEndDocument();

                    throw new ArgumentOutOfRangeException("index");
                }
            }
            set
            {
                throw new NotSupportedException("RawBsonArray instances are immutable.");
            }
        }
        /// <summary>
        /// Gets the index of a value in the array.
        /// </summary>
        /// <param name="value">The value to search for.</param>
        /// <param name="index">The zero based index at which to start the search.</param>
        /// <param name="count">The number of elements to search.</param>
        /// <returns>The zero based index of the value (or -1 if not found).</returns>
        public override int IndexOf(BsonValue value, int index, int count)
        {
            ThrowIfDisposed();
            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                var context = BsonDeserializationContext.CreateRoot(bsonReader);

                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    bsonReader.SkipName();
                    if (i >= index)
                    {
                        if (count == 0)
                        {
                            return -1;
                        }

                        if (DeserializeBsonValue(context).Equals(value))
                        {
                            return i;
                        }

                        count--;
                    }
                    else
                    {
                        bsonReader.SkipValue();
                    }

                    i++;
                }
                bsonReader.ReadEndDocument();

                return -1;
            }
        }
        /// <summary>
        /// Gets an element of this document.
        /// </summary>
        /// <param name="index">The zero based index of the element.</param>
        /// <returns>
        /// The element.
        /// </returns>
        public override BsonElement GetElement(int index)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            ThrowIfDisposed();

            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                var context = BsonDeserializationContext.CreateRoot(bsonReader);
                
                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (i == index)
                    {
                        var name = bsonReader.ReadName();
                        var value = DeserializeBsonValue(context);
                        return new BsonElement(name, value);
                    }

                    bsonReader.SkipName();
                    bsonReader.SkipValue();
                    i++;
                }
                bsonReader.ReadEndDocument();

                throw new ArgumentOutOfRangeException("index");
            }
        }
        /// <summary>
        /// Tries to get the value of an element of this document.
        /// </summary>
        /// <param name="name">The name of the element.</param>
        /// <param name="value">The value of the element.</param>
        /// <returns>
        /// True if an element with that name was found.
        /// </returns>
        public override bool TryGetValue(string name, out BsonValue value)
        {
            ThrowIfDisposed();
            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                var context = BsonDeserializationContext.CreateRoot(bsonReader);
                
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        value = DeserializeBsonValue(context);
                        return true;
                    }

                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                value = null;
                return false;
            }
        }
        /// <summary>
        /// Tests whether the document contains an element with the specified name.
        /// </summary>
        /// <param name="name">The name of the element to look for.</param>
        /// <returns>
        /// True if the document contains an element with the specified name.
        /// </returns>
        public override bool Contains(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            ThrowIfDisposed();

            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        return true;
                    }
                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                return false;
            }
        }
        /// <summary>
        /// Gets the index of a value in the array.
        /// </summary>
        /// <param name="value">The value to search for.</param>
        /// <param name="index">The zero based index at which to start the search.</param>
        /// <param name="count">The number of elements to search.</param>
        /// <returns>The zero based index of the value (or -1 if not found).</returns>
        public override int IndexOf(BsonValue value, int index, int count)
        {
            ThrowIfDisposed();
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    bsonReader.SkipName();
                    if (i >= index)
                    {
                        if (count == 0)
                        {
                            return -1;
                        }

                        if (DeserializeBsonValue(bsonReader).Equals(value))
                        {
                            return i;
                        }

                        count--;
                    }
                    else
                    {
                        bsonReader.SkipValue();
                    }

                    i++;
                }
                bsonReader.ReadEndDocument();

                return -1;
            }
        }
        // public indexers
        /// <summary>
        /// Gets or sets a value by position.
        /// </summary>
        /// <param name="index">The position.</param>
        /// <returns>The value.</returns>
        public override BsonValue this[int index]
        {
            get
            {
                if (index < 0)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                ThrowIfDisposed();

                using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
                {
                    bsonReader.ReadStartDocument();
                    var i = 0;
                    while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                    {
                        bsonReader.SkipName();
                        if (i == index)
                        {
                            return DeserializeBsonValue(bsonReader);
                        }

                        bsonReader.SkipValue();
                        i++;
                    }
                    bsonReader.ReadEndDocument();

                    throw new ArgumentOutOfRangeException("index");
                }
            }
            set
            {
                throw new NotSupportedException("RawBsonArray instances are immutable.");
            }
        }
        /// <summary>
        /// Tries to get the value of an element of this document.
        /// </summary>
        /// <param name="name">The name of the element.</param>
        /// <param name="value">The value of the element.</param>
        /// <returns>
        /// True if an element with that name was found.
        /// </returns>
        public override bool TryGetValue(string name, out BsonValue value)
        {
            ThrowIfDisposed();
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        value = DeserializeBsonValue(bsonReader);
                        return true;
                    }

                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                value = null;
                return false;
            }
        }
        /// <summary>
        /// Gets the value of an element.
        /// </summary>
        /// <param name="index">The zero based index of the element.</param>
        /// <returns>
        /// The value of the element.
        /// </returns>
        public override BsonValue GetValue(int index)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            ThrowIfDisposed();

            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    bsonReader.SkipName();
                    if (i == index)
                    {
                        return DeserializeBsonValue(bsonReader);
                    }

                    bsonReader.SkipValue();
                    i++;
                }
                bsonReader.ReadEndDocument();

                throw new ArgumentOutOfRangeException("index");
            }
        }