1 year ago

#345287

test-img

Inhinias

.Net MongDB Driver more than 5x slower than pymongo equivalent

I have a collection with ~8.5k documents. This Python code gets the last added document in ~50-70ms:

from pymongo import MongoClient
import pymongo, time

client = MongoClient(connectionString)
db = client[databaseName]
collection = db[collectionName]

tic = time.perf_counter()
collection.find({}).sort([("Timestamp", pymongo.DESCENDING)]).limit(1)[0]
toc = time.perf_counter()
print("Took {0} seconds".format(toc - tic))

While this equivalent C# code requires ~300-400ms:

var start = Environment.TickCount;
var doc = dbcollection.Find(new BsonDocument()).Sort(Builders<BsonDocument>.Sort.Descending("Timestamp")).Limit(1).ToList();
var end = Environment.TickCount;
Console.WriteLine(end-start);

My question is: Where is this performance difference coming from and how can i improve the C# speeds?

Other things to mention:

  • The Timestamp field is indexed
  • .Net MongoDB driver version is: 2.14.1
  • pymongo version: 4.0.2
  • MongoDB version: 4.4.0 Community
  • Avg. document size is ~840 Bytes

c#

mongodb

pymongo

mongodb-.net-driver

0 Answers

Your Answer

Accepted video resources