I want to query mongodb database to find all value under a specific field between two dates using c#. Can you help how to query a specific field in a collection?
this is my collection "Devise" and i try to query all the value of "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; }
}
this is my query code
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;
}
}
I assumed that by "find all value" you mean all distinct values from field high
. If so, you could do this as example below:
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));
See MongoCollection.Distinct and db.collection.distinct() for more information.
The above snippet is tested in C# Driver v2.2.3 with MongoDB v3.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.