Hi everyone,
I’ve been learning MongoDB for a while and recently encountered a strange issue when using the C# driver. Specifically, when inserting a large number of documents at once, the execution time becomes abnormally long. However, this issue does not occur when using the Node.js driver. I would like to know if there are any issues in how I’m using it currently.
For small-scale operations (insert, read, delete), both Node.js and C# perform similarly in terms of execution time.
Node.js insert 10 documents: 24 ms
Node.js read 10 documents: 20 ms
Node.js delete 10 documents: 21 ms
C# insert 10 documents: 40 ms
C# read 10 documents: 26 ms
C# delete 10 documents: 21 ms
But once the number of documents reaches a certain threshold, the insert operation in C# becomes significantly slower(almost but not always).
Node.js insert 100 documents: 28 ms
Node.js read 100 documents: 22 ms
Node.js delete 100 documents: 24 ms
C# insert 100 documents: 848 ms
C# read 100 documents: 27 ms
C# delete 100 documents: 24 ms
Here is the code I used for testing:
C# Driver: v3.3
Node.js Driver: v6.15
C# Driver
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core.Events;
using System.Diagnostics;
var settings = MongoClientSettings.FromConnectionString("your connection string here");
settings.ClusterConfigurator = cb =>
{
cb.Subscribe<ConnectionOpenedEvent>(e =>
{
Console.WriteLine($"[{e.Timestamp:yyyy-MM-dd HH:mm:ss.fffffff}][ConnectionOpened] {e.ConnectionId}");
});
cb.Subscribe<ConnectionClosedEvent>(e =>
{
Console.WriteLine($"[{e.Timestamp:yyyy-MM-dd HH:mm:ss.fffffff}][ConnectionClosed] {e.ConnectionId}");
});
cb.Subscribe<ConnectionSendingMessagesEvent>(e =>
{
Console.WriteLine($"[{e.Timestamp:yyyy-MM-dd HH:mm:ss.fffffff}][ConnectionSendingMessagesEvent]");
});
cb.Subscribe<ConnectionSentMessagesEvent>(e =>
{
Console.WriteLine($"[{e.Timestamp:yyyy-MM-dd HH:mm:ss.fffffff}][ConnectionSentMessagesEvent] Duration: {e.Duration}, NetworkDuration: {e.NetworkDuration}, SerializationDuration: {e.SerializationDuration}");
});
cb.Subscribe<ConnectionReceivingMessageEvent>(e =>
{
Console.WriteLine($"[{e.Timestamp:yyyy-MM-dd HH:mm:ss.fffffff}][ConnectionReceivingMessageEvent]");
});
cb.Subscribe<ConnectionReceivedMessageEvent>(e =>
{
Console.WriteLine($"[{e.Timestamp:yyyy-MM-dd HH:mm:ss.fffffff}][ConnectionReceivedMessageEvent] Duration: {e.Duration}, NetworkDuration: {e.NetworkDuration}, DeserializationDuration: {e.DeserializationDuration}");
});
};
var client = new MongoClient(settings);
var database = client.GetDatabase("testdbCSharp");
var collection = database.GetCollection<BsonDocument>("testCollection");
await database.RunCommandAsync((Command<BsonDocument>)"{ping:1}");
int count = 100;
// Insert Test
var sw = Stopwatch.StartNew();
var insertDocs = Enumerable.Range(0, count)
.Select(i => new BsonDocument("message", "Hello, World!"));
await collection.InsertManyAsync(insertDocs);
sw.Stop();
Console.WriteLine($"C# insert {count} documents: {sw.ElapsedMilliseconds} ms");
// Read Test
sw.Restart();
var readDocs = await collection.Find(FilterDefinition<BsonDocument>.Empty).ToListAsync();
sw.Stop();
Console.WriteLine($"C# read {readDocs.Count} documents: {sw.ElapsedMilliseconds} ms");
// Delete Test
sw.Restart();
var deleteResult = await collection.DeleteManyAsync(FilterDefinition<BsonDocument>.Empty);
sw.Stop();
Console.WriteLine($"C# delete {deleteResult.DeletedCount} documents: {sw.ElapsedMilliseconds} ms");
Node.js Driver
const { MongoClient } = require('mongodb');
async function run() {
const uri = "your connection string here";
const client = new MongoClient(uri, { monitorCommands: true });
client.on('connectionCreated', (event) => {
console.log('[ConnectionCreated]', event.connectionId, event.address, event.time);
});
client.on('connectionClosed', (event) => {
console.log('[ConnectionClosed]', event.connectionId, event.address, event.time);
});
client.on('commandStarted', (event) => {
console.log(`[commandStarted]`, event.commandName);
});
client.on('commandSucceeded', (event) => {
console.log(`[commandSucceeded]`, event.commandName, event.duration);
});
await client.connect();
const db = client.db("testdbJavaScript");
const collection = db.collection("testCollection");
const count = 100;
// Insert Test
const insertDocs = [];
for (let i = 0; i < count; i++) {
insertDocs.push({ message: "Hello, World!" });
}
let start = Date.now();
await collection.insertMany(insertDocs);
let elapsed = Date.now() - start;
console.log(`Node.js insert ${count} documents: ${elapsed} ms`);
// Read Test
start = Date.now();
const foundDocs = await collection.find({}).toArray();
elapsed = Date.now() - start;
console.log(`Node.js read ${foundDocs.length} documents: ${elapsed} ms`);
// Delete Test
start = Date.now();
const deleteResult = await collection.deleteMany({});
elapsed = Date.now() - start;
console.log(`Node.js delete ${deleteResult.deletedCount} documents: ${elapsed} ms`);
await client.close();
}
run().catch(console.dir);
Full executing log in C# Driver:
[2025-05-06 03:31:15.3411040][ConnectionSendingMessagesEvent]
[2025-05-06 03:31:15.3472857][ConnectionSentMessagesEvent]Duration: 00:00:00.0015050, NetworkDuration: 00:00:00.0011593, SerializationDuration: 00:00:00.0003457
[2025-05-06 03:31:15.3479549][ConnectionReceivingMessageEvent]
[2025-05-06 03:31:15.3647306][ConnectionReceivedMessageEvent]Duration: 00:00:00.0165344, NetworkDuration: 00:00:00.0165234, DeserializationDuration: 00:00:00.0000110
[2025-05-06 03:31:15.3675802][ConnectionSendingMessagesEvent]
[2025-05-06 03:31:15.3677290][ConnectionSentMessagesEvent]Duration: 00:00:00.0000970, NetworkDuration: 00:00:00.0000607, SerializationDuration: 00:00:00.0000363
[2025-05-06 03:31:15.3677923][ConnectionReceivingMessageEvent]
[2025-05-06 03:31:15.3835204][ConnectionReceivedMessageEvent]Duration: 00:00:00.0156887, NetworkDuration: 00:00:00.0156740, DeserializationDuration: 00:00:00.0000147
[2025-05-06 03:31:15.3905524][ConnectionSendingMessagesEvent]
[2025-05-06 03:31:15.3907241][ConnectionSentMessagesEvent]Duration: 00:00:00.0000999, NetworkDuration: 00:00:00.0000724, SerializationDuration: 00:00:00.0000275
[2025-05-06 03:31:15.3907762][ConnectionReceivingMessageEvent]
[2025-05-06 03:31:15.4065343][ConnectionReceivedMessageEvent]Duration: 00:00:00.0157337, NetworkDuration: 00:00:00.0157179, DeserializationDuration: 00:00:00.0000158
[2025-05-06 03:31:15.4072797][ConnectionOpened] { ServerId : { ClusterId : 1, EndPoint : " Your EndPoint" }, LocalValue : 7, ServerValue : "338038" }
[2025-05-06 03:31:15.4153684][ConnectionSendingMessagesEvent]
[2025-05-06 03:31:15.4156596][ConnectionSentMessagesEvent]Duration: 00:00:00.0001825, NetworkDuration: 00:00:00.0000707, SerializationDuration: 00:00:00.0001118
[2025-05-06 03:31:15.4160661][ConnectionReceivingMessageEvent]
[2025-05-06 03:31:15.4320950][ConnectionReceivedMessageEvent]Duration: 00:00:00.0159925, NetworkDuration: 00:00:00.0154494, DeserializationDuration: 00:00:00.0005431
[2025-05-06 03:31:15.4511362][ConnectionSendingMessagesEvent]
[2025-05-06 03:31:15.4523578][ConnectionSentMessagesEvent]Duration: 00:00:00.0010472, NetworkDuration: 00:00:00.0001143, SerializationDuration: 00:00:00.0009329
[2025-05-06 03:31:15.4525413][ConnectionReceivingMessageEvent]
[2025-05-06 03:31:16.2724546][ConnectionReceivedMessageEvent]Duration: 00:00:00.8198764, NetworkDuration: 00:00:00.8198157, DeserializationDuration: 00:00:00.0000607
C# insert 100 documents: 841 ms
[2025-05-06 03:31:16.2849304][ConnectionSendingMessagesEvent]
[2025-05-06 03:31:16.2853665][ConnectionSentMessagesEvent]Duration: 00:00:00.0003116, NetworkDuration: 00:00:00.0000983, SerializationDuration: 00:00:00.0002133
[2025-05-06 03:31:16.2854645][ConnectionReceivingMessageEvent]
[2025-05-06 03:31:16.3026622][ConnectionReceivedMessageEvent]Duration: 00:00:00.0171346, NetworkDuration: 00:00:00.0169969, DeserializationDuration: 00:00:00.0001377
C# read 100 documents: 28 ms
[2025-05-06 03:31:16.3085461][ConnectionSendingMessagesEvent]
[2025-05-06 03:31:16.3088250][ConnectionSentMessagesEvent]Duration: 00:00:00.0002254, NetworkDuration: 00:00:00.0000568, SerializationDuration: 00:00:00.0001686
[2025-05-06 03:31:16.3088699][ConnectionReceivingMessageEvent]
[2025-05-06 03:31:16.3347279][ConnectionReceivedMessageEvent]Duration: 00:00:00.0258226, NetworkDuration: 00:00:00.0257863, DeserializationDuration: 00:00:00.0000363
C# delete 100 documents: 28 ms