Update:
I’ve done a projection on the fields as:
{
utcCreated,
longitude,
latitude
}
and got an approximate execution time of 7 seconds, which is by far less than the initial 24 seconds, BUT a long way from the avg. of 120ms when retrieved from MS SQL.
Another (ChatGPT-ish) attempt is to go with aggregation that might outperform the ussal Find().ToListAsync():
var filter = Builders<GpsTSDto>.Filter.And(
Builders<GpsTSDto>.Filter.Gt(d => d.MetaData.CompanyId, 1000),
Builders<GpsTSDto>.Filter.Gte(d => d.MetaData.AssetId, 26657),
Builders<GpsTSDto>.Filter.Lte(d => d.MetaData.AssetId, 26659),
Builders<GpsTSDto>.Filter.Gte(d => d.UtcCreated, new DateTime(2025, 04, 16, 0, 0, 0, DateTimeKind.Utc)),
Builders<GpsTSDto>.Filter.Lte(d => d.UtcCreated, new DateTime(2025, 05, 10, 21, 50, 0, DateTimeKind.Utc))
);
var pipeline = new[]
{
new BsonDocument("$match", filter.Render(
BsonSerializer.SerializerRegistry.GetSerializer<GpsTSDto>(),
BsonSerializer.SerializerRegistry
)),
new BsonDocument("$project", new BsonDocument
{
{ "utcCreated", 1 },
{ "latitude", new BsonDocument("$round", new BsonArray { "$latitude", 5 }) },
{ "longitude", new BsonDocument("$round", new BsonArray { "$longitude", 5 }) },
{ "_id", 0 }
})
};
return await _gpsTSCollection
.Aggregate<LocationTSDto>(pipeline)
.ToListAsync()
.ConfigureAwait(false);
BUT still, 7 seconds