Having Difficulty Using MongoDb C# Driver's Sample()

前端 未结 1 1488
逝去的感伤
逝去的感伤 2021-01-22 21:06

I am trying to get some random items from the database using the Sample() method. I updated to the latest version of the driver and copied the using statements from the linked e

相关标签:
1条回答
  • 2021-01-22 21:47

    Sample is only available from an aggregation. You need to start with Aggregate, not Find. I believe it's also available in Linq.

    UPDATE: Looks like we don't have a method for it specifically. However, you can use the AppendStage method.

    mongoCollection.Aggregate(sampleFilter)
                   .AppendStage<BsonDocument>("{ $sample: { size: 3 } }");
    
    0 讨论(0)
提交回复
热议问题