英文:
limit results of mongodb query in query itself
问题
可以使用mongodb查询中的limit函数来限制结果集。
https://stackoverflow.com/questions/40356608/mongodb-limit-find-results
但我正在创建一个API,可以通过发送原始的BsonDocument来查询mongodb,该API使用C#的MongoClient来查找结果。
public async Task<string> Query(string database, string collection, string qry)
{
var db = Connection.Client.GetDatabase(database);
var col = db.GetCollection<BsonDocument>(collection);
var filter = BsonDocument.Parse(qry);
var result = await col.FindAsync(filter);
string json = "";
await result.ForEachAsync(b => json += toJson(b));
return json;
}
但是是否有办法在BsonDocument本身中限制结果?也许可以添加一个过滤器?
英文:
It's possible to limit results of a mongodb query with the limit function
https://stackoverflow.com/questions/40356608/mongodb-limit-find-results
But I am creating an API with with you can query mongodb by sending a raw BsonDocument and the api uses C# MongoClient to find results.
public async Task<string> Query(string database, string collection, string qry)
{
var db = Connection.Client.GetDatabase(database);
var col = db.GetCollection<BsonDocument>(collection);
var filter = BsonDocument.Parse(qry);
var result = await col.FindAsync(filter);
string json = "";
await result.ForEachAsync(b => json += toJson(b));
return json;
}
But is there a way to limit results in the BsonDocument itself? Add a filter maybe?
答案1
得分: 1
从 IMongoCollectionExtensions.FindAsync<TDocument> 方法 (IMongoCollection<TDocument>, Expression<Func<TDocument, Boolean>>, FindOptions<TDocument, TDocument>, CancellationToken)
中,您可以使用 FindOptions
中的 Limit
属性 来限制返回的文档数量。
var result = await col.FindAsync(filter, new FindOptions<BsonDocument>
{
Limit = 100 // 要返回的文档数量
});
英文:
From the IMongoCollectionExtensions.FindAsync<TDocument> Method (IMongoCollection<TDocument>, Expression<Func<TDocument, Boolean>>, FindOptions<TDocument, TDocument>, CancellationToken)
method, you can provide the FindOption
with Limit
property to limit the number of returned document(s).
var result = await col.FindAsync(filter, new FindOptions<BsonDocument>
{
Limit = 100 // Number of document(s) to be returned
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论