Skip to content

Instantly share code, notes, and snippets.

@SebastianStehle
Created July 9, 2023 16:38
Show Gist options
  • Save SebastianStehle/3d3b1bd18da989c1b285c9e4db1c57b0 to your computer and use it in GitHub Desktop.
Save SebastianStehle/3d3b1bd18da989c1b285c9e4db1c57b0 to your computer and use it in GitHub Desktop.
using MongoDB.Bson;
using MongoDB.Driver;
using System.Diagnostics;
namespace MongoTest;
public class Program
{
public static async Task Main()
{
var mongoClient = new MongoClient("mongodb://localhost");
var mongoDatabase = mongoClient.GetDatabase("QueryTest");
var mongoCollection = mongoDatabase.GetCollection<Parent>("QueryTest2");
if (await mongoCollection.CountDocumentsAsync(new BsonDocument()) <= 10)
{
var insert = new List<Parent>();
for (var i = 0; i < 1000; i++)
{
insert.Add(new Parent());
}
await mongoCollection.InsertManyAsync(insert);
}
const int NumAttempts = 5;
const int NumItems = 20;
var watch1 = Stopwatch.StartNew();
var count1 = 0;
for (var j = 0; j < NumAttempts; j++)
{
var result =
await mongoCollection.Find(x => x.Child.Number3 == 3)
.Limit(NumItems)
.ToListAsync();
count1 += result.Count;
}
watch1.Stop();
Console.WriteLine("Query Normal {0}: {1}", watch1.Elapsed / NumAttempts, count1);
var watch2 = Stopwatch.StartNew();
var count2 = 0;
for (var j = 0; j < NumAttempts; j++)
{
var result =
await mongoCollection.Find(x => x.Child.Number3 == 3)
.Project<Parent>(
Builders<Parent>.Projection
.Include(x => x.Id)
.Include(x => x.Child.Number0)
.Include(x => x.Child.Number1)
.Include(x => x.Child.Number2)
.Include(x => x.Child.Text))
.Limit(NumItems)
.ToListAsync();
count2 += result.Count;
}
watch2.Stop();
Console.WriteLine("Query Limited {0}: {1}", watch2.Elapsed / NumAttempts, count2);
}
}
class Parent
{
public Guid Id { get; set; } = Guid.NewGuid();
public Child Child { get; set; } = Child.Create();
}
public class Child
{
public string? Text { get; set; }
public int Number0 { get; set; }
public int Number1 { get; set; }
public int Number2 { get; set; }
public int Number3 { get; set; }
public Dictionary<string, Child>? Object { get; set; }
public List<Child>? List { get; set; }
public static Child Create(int level = 0)
{
var prefix = level * 1000;
var result = new Child
{
Number0 = prefix + 0,
Number1 = prefix + 1,
Number2 = prefix + 2,
Number3 = prefix + 3,
Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
};
if (level < 3)
{
result.Object = new Dictionary<string, Child>();
for (var i = 0; i < 10; i++)
{
result.Object[i.ToString()] = Create(level + 1);
}
result.List = new List<Child>();
for (var i = 0; i < 10; i++)
{
result.List.Add(Create(level + 1));
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment