[英]How to find the min value of a specific field in MongoDB using C# driver 2.10.4
[英]Query mongodb database to find all value for a specific field c#
我想查詢mongodb數據庫以使用c#在兩個日期之間的特定字段下查找所有值。 您可以幫助查詢集合中的特定字段嗎?
這是我的收藏“ Devise”,我嘗試查詢“ high”的所有值
public class Devise
{
public string parité { get; set; }
public float low { get; set; }
public float high { get; set; }
public DateTime date_observation { get; set; }
}
這是我的查詢代碼
public class Devisedata
{
private IMongoDatabase _database;
public async System.Threading.Tasks.Task<List<Devise>> GetDataDevise(DateTime inputDate1, DateTime inputDate2)
{
var collection = _database.GetCollection<Devise>("datafinance");
var builder = Builders<Devise>.Filter;
var filter = builder.Gte("date_observation", inputDate1) &
builder.Lt("date_observation", inputDate2.AddDays(1));
var list = await collection.Find(filter).ToListAsync();
return list;
}
}
我假設通過“查找所有值”來表示字段high
所有不同值。 如果是這樣,您可以按照以下示例進行操作:
var builder = Builders<Devise>.Filter;
var filter = builder.Gte("date_observation", inputDate1) &
builder.Lt("date_observation", inputDate2.AddDays(1));
var cursor = await collection.DistinctAsync<double>("high", match);
await cursor.ForEachAsync(doc => Console.WriteLine(doc));
有關更多信息,請參見MongoCollection.Distinct和db.collection.distinct() 。
上面的代碼段已在C#驅動程序 v2.2.3和MongoDB v3.2中進行了測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.