我正在使用mongoDB开发asp.NET6.0.
我有一个查询来过滤wine 店结果.
public async Task<List<HotelResultDocument>> GetHotelResults(List<string> rateKeys)
{
var filter = Builders<HotelResultDocument>.Filter.In(x => x.RateKey, rateKeys);
filter &= Builders<HotelResultDocument>.Filter.Eq(x => x.RateType, "BOOKABLE");
filter &= Builders<HotelResultDocument>.Filter.Gt(x => x.ExpiredAt, DateTime.Now);
return await _hotelResultsCollection.Find(filter).ToListAsync();
}
在这里,我的问题是,对于"可预订"类型的同一rateKEy,有多个回复.我只想最后添加一个与"可预订"类型的rateKEy集合.
如何过滤以获得上述响应?
public class HotelResultDocument
{
public string Id { get; set; }
public string Reference { get; set; }
public string Token { get; set; }
public int HotelCode { get; set; }
public string RateKey { get; set; }
public string RateClass { get; set; }
public string RateType { get; set; }
public double Net { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ExpiredAt { get; set; }
}
Note : If I sent four rateKeys I have to find the hotels for each rateKeys (if the hotel satisfied above conditions.) But using above query I got more then one hotel for a rateKey. I have to find the lastly added hotel for a particular rateKey.
编辑:
"hotel" : [
{
"hotelCode" : 2,
"CreatedAt" :{26/07/2022 03:50:26},
"RateKey" : 1,
},
{
"hotelCode" : 2,
"CreatedAt" :{26/07/2022 03:51:26},
"RateKey" : 1,
},
{
"hotelCode" : 2,
"CreatedAt" :{26/07/2022 03:52:26},
"RateKey" : 1,
},
{
"hotelCode" : 2,
"CreatedAt" :{26/07/2022 03:54:26},
"RateKey" : 1,
},
{
"hotelCode" : 4,
"CreatedAt" :{26/07/2022 03:52:26},
"RateKey" : 5,
},
{
"hotelCode" : 4,
"CreatedAt" :{26/07/2022 03:55:26},
"RateKey" : 5,
}
]
预期响应:
"hotel" : [
{
"hotelCode" : 2,
"CreatedAt" :{26/07/2022 03:54:26},
"RateKey" : 1,
},
{
"hotelCode" : 4,
"CreatedAt" :{26/07/2022 03:55:26},
"RateKey" : 5,
}
]
谁来帮帮我.