Repository: microsoft/semantic-kernel
File: dotnet/src/VectorData/MongoDB/MongoCollection.cs (C#)

Code before:
                    yield return new VectorSearchResult<TRecord>(record, score);
                }

                skipCounter++;
            }
        }
    }

    private FilterDefinition<BsonDocument> GetFilterById(TKey id)
    {
        // Use cached key serialization info but fall back to BsonValueFactory for dynamic mapper.
        var bsonValue = this._keySerializationInfo?.SerializeValue(id) ?? BsonValueFactory.Create(id);
        return Builders<BsonDocument>.Filter.Eq(MongoConstants.MongoReservedKeyPropertyName, bsonValue);
    }

    private FilterDefinition<BsonDocument> GetFilterByIds(IEnumerable<TKey> ids)
    {
        // Use cached key serialization info but fall back to BsonValueFactory for dynamic mapper.
        var bsonValues = this._keySerializationInfo?.SerializeValues(ids) ?? (BsonArray)BsonValueFactory.Create(ids);
        return Builders<BsonDocument>.Filter.In(MongoConstants.MongoReservedKeyPropertyName, bsonValues);
    }

    private BsonSerializationInfo GetKeySerializationInfo()
    {
        var documentSerializer = BsonSerializer.LookupSerializer<TRecord>();
        Verify.NotNull(documentSerializer, $"BsonSerializer not found for type '{typeof(TRecord)}'");

        if (documentSerializer is not IBsonDocumentSerializer bsonDocumentSerializer)
        {
            throw new InvalidOperationException($"BsonSerializer for type '{typeof(TRecord)}' does not implement IBsonDocumentSerializer");
        }

        if (!bsonDocumentSerializer.TryGetMemberSerializationInfo(this._model.KeyProperty.ModelName, out var keySerializationInfo))
        {
            throw new InvalidOperationException($"BsonSerializer for type '{typeof(TRecord)}' does not recognize key property {this._model.KeyProperty.ModelName}");
        }

        return keySerializationInfo;
    }

    private async Task<bool> InternalCollectionExistsAsync(CancellationToken cancellationToken)
    {
        var filter = new BsonDocument("name", this.Name);
        var options = new ListCollectionNamesOptions { Filter = filter };

        using var cursor = await this._mongoDatabase.ListCollectionNamesAsync(options, cancellationToken: cancellationToken).ConfigureAwait(false);

        return await cursor.AnyAsync(cancellationToken).ConfigureAwait(false);
    }

    private Task RunOperationAsync(string operationName, Func<Task> operation)

Review comment (suggestion):
This will throw an ArgumentException with the parameter name of `"BsonSerializer not found for type '{typeof(TRecord)}'`.
This should probably also just be an if statement with InvalidOperationException.

```suggestion
if (documentSerializer is null)
{
    throw new InvalidOperationException($"BsonSerializer not found for type '{typeof(TRecord)}'");
}
```

Code after:
                    yield return new VectorSearchResult<TRecord>(record, score);
                }

                skipCounter++;
            }
        }
    }

    private FilterDefinition<BsonDocument> GetFilterById(TKey id)
    {
        // Use cached key serialization info but fall back to BsonValueFactory for dynamic mapper.
        var bsonValue = this._keySerializationInfo?.SerializeValue(id) ?? BsonValueFactory.Create(id);
        return Builders<BsonDocument>.Filter.Eq(MongoConstants.MongoReservedKeyPropertyName, bsonValue);
    }

    private FilterDefinition<BsonDocument> GetFilterByIds(IEnumerable<TKey> ids)
    {
        // Use cached key serialization info but fall back to BsonValueFactory for dynamic mapper.
        var bsonValues = this._keySerializationInfo?.SerializeValues(ids) ?? (BsonArray)BsonValueFactory.Create(ids);
        return Builders<BsonDocument>.Filter.In(MongoConstants.MongoReservedKeyPropertyName, bsonValues);
    }

    private BsonSerializationInfo GetKeySerializationInfo()
    {
        var documentSerializer = BsonSerializer.LookupSerializer<TRecord>();
        if (documentSerializer is null)
        {
            throw new InvalidOperationException($"BsonSerializer not found for type '{typeof(TRecord)}'");
        }

        if (documentSerializer is not IBsonDocumentSerializer bsonDocumentSerializer)
        {
            throw new InvalidOperationException($"BsonSerializer for type '{typeof(TRecord)}' does not implement IBsonDocumentSerializer");
        }

        if (!bsonDocumentSerializer.TryGetMemberSerializationInfo(this._model.KeyProperty.ModelName, out var keySerializationInfo))
        {
            throw new InvalidOperationException($"BsonSerializer for type '{typeof(TRecord)}' does not recognize key property {this._model.KeyProperty.ModelName}");
        }

        return keySerializationInfo;
    }

    private async Task<bool> InternalCollectionExistsAsync(CancellationToken cancellationToken)
    {
        var filter = new BsonDocument("name", this.Name);
        var options = new ListCollectionNamesOptions { Filter = filter };

        using var cursor = await this._mongoDatabase.ListCollectionNamesAsync(options, cancellationToken: cancellationToken).ConfigureAwait(false);

        return await cursor.AnyAsync(cancellationToken).ConfigureAwait(false);