Created
May 22, 2020 07:42
-
-
Save SebastianStehle/db55773603b88c71d466d14f7a39bfce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LoremNET; | |
using MongoDB.Bson; | |
using MongoDB.Driver; | |
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace ConsoleApp6 | |
{ | |
class Program | |
{ | |
class TestDocument | |
{ | |
public string Id { get; set; } | |
public string Text { get; set; } | |
public int Number { get; set; } | |
} | |
static void Main(string[] args) | |
{ | |
var mongoClient = new MongoClient("mongodb://localhost"); | |
var mongoDatabase = mongoClient.GetDatabase("Test"); | |
var mongoCollection = mongoDatabase.GetCollection<TestDocument>("test"); | |
var generate = false; | |
if (generate) | |
{ | |
var random = new Random(); | |
var pos = Console.CursorTop; | |
for (var i = 0; i < 100000; i++) | |
{ | |
var document = new TestDocument | |
{ | |
Id = Guid.NewGuid().ToString(), | |
Number = random.Next(int.MaxValue), | |
Text = Lorem.Paragraph(200, 20) | |
}; | |
mongoCollection.InsertOne(document); | |
Console.SetCursorPosition(0, pos); | |
Console.WriteLine("Documents: {0}", i); | |
} | |
} | |
for (var i = 0; i < 10; i++) | |
{ | |
var w = Stopwatch.StartNew(); | |
var test1 = mongoCollection | |
.Aggregate() | |
.Project<TestDocument>(Builders<TestDocument>.Projection.Include(x => x.Id).Include(x => x.Number)) | |
.SortBy(x => x.Number) | |
.Skip(5).Limit(200).ToList(); | |
var ids = test1.Select(x => x.Id).ToList(); | |
var all = mongoCollection.Find(Builders<TestDocument>.Filter.In("_id", ids)).ToList(); | |
w.Stop(); | |
Console.WriteLine("Elapsed {0}", w.Elapsed); | |
} | |
for (var i = 0; i < 10; i++) | |
{ | |
var w = Stopwatch.StartNew(); | |
var test2 = mongoCollection.Find(new BsonDocument()).Project(Builders<TestDocument>.Projection.Exclude(x => x.Text)).Skip(1).SortBy(x => x.Number).Limit(200).ToList(); | |
w.Stop(); | |
Console.WriteLine("Elapsed {0}", w.Elapsed); | |
} | |
var test3 = mongoCollection.Find(new BsonDocument()).Project(Builders<TestDocument>.Projection.Exclude(x => x.Text)).Skip(1000).SortBy(x => x.Number).Limit(200).ToList(); | |
var test4 = mongoCollection.Find(new BsonDocument()).Skip(1000).SortBy(x => x.Number).Limit(200).ToList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment