MongoDB:将$ sample与C#驱动程序一起使用 [英] MongoDB: Using $sample with C# driver

查看:59
本文介绍了MongoDB:将$ sample与C#驱动程序一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MongoDB C#驱动程序(2.4.4)表达以下查询:

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();

我不能将sample放入.Aggregate(AggregateOptions options = null)并将其放入.Group(...)显然是错误的.也没有像.Sample()这样的方法.

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.

请帮忙.预先谢谢你.

推荐答案

简单

var randEl = await collection.AsQueryable().Sample(1).FirstOrDefaultAsync();

别忘了添加

using MongoDB.Driver.Linq;

这篇关于MongoDB:将$ sample与C#驱动程序一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆