I'm trying to express the following query using the MongoDB C# driver (2.4.4):
db.media.aggregate({ $sample: { size: 1 }})
This what I have so far:
BsonDocument sample = new BsonDocument
{
{ "$sample", new BsonDocument { { "size", 1 } } }
};
MongoBlob mongoBlob = await _collection
.Aggregate()
.Group<MongoBlob>(sample)
.FirstOrDefaultAsync();
I cannot put the sample
to .Aggregate(AggregateOptions options = null)
and putting it into the .Group(...)
is obviously wrong. There is also no any like a .Sample()
method.
Please, help. Thank you in advance.
I believe it should be
If this doesn't work then let me know. Don't have windows system up right now to confirm
Edit (7-AUG):
Turns out its not that simple. The sample method doesn't exists in current driver. The classes which handle this are
internal
so no straight forward way to inherit. So worked out a solution based on reflection and a hack. Where you add a Stage to the pipeline and then edit it through reflection. Below is a working sample of my demo codeusing System; using MongoDB.Bson; using MongoDB.Driver; using System.Reflection;
Simply,
Do not forget add