Complexity using the MongoDb C # () driver example

I am trying to get some random items from a database using the Sample () method . I updated to the latest driver and copied using statements from a related example. However, something is not working, and I hope this will be some simple mistake on my part. All relevant information is in the image below:

enter image description here

Edit:

Greg, I read aggregated documents here and the raw db doc method here , but I still don't get it. The last two lines are my attempts, I have never used aggregation before:

            var mongoCollection = GetMongoCollection<BsonDocument>(collectionName);
            long[] locationIds = new long[] { 1, 2, 3, 4, 5 };
            var locationsArray = new BsonArray();
            foreach (long l in locationIds) { locationsArray.Add(l); };
            FilterDefinition<BsonDocument> sampleFilter = Builders<BsonDocument>.Filter.In("LocationId", locationsArray);
            var findSomething = mongoCollection.Find(sampleFilter); //this works, the two attempts below don't.
            var aggregateSomething = mongoCollection.Aggregate(sampleFilter).Sample(25);
            var aggregateSomething2 = mongoCollection.Aggregate().Match(sampleFilter).Sample(25);
+2
source share
1 answer

. Aggregate, Find. , Linq.

UPDATE: , . AppendStage.

mongoCollection.Aggregate(sampleFilter)
               .AppendStage<BsonDocument>("{ $sample: { size: 3 } }");
+4

Source: https://habr.com/ru/post/1687068/


All Articles