我正在使用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,
}
]

谁来帮帮我.

推荐答案

我相信通过聚合管道可以实现这一点.

Updated based on Post Owner's requirements

  1. $match-过滤文档.

  2. $sort-按CreatedAt降序排列.

  3. $group-按RateKey分组,将第一个文档作为hotel.

  4. $replaceWith-将输入文档替换为hotel.

MongoDB查询

db.collection.aggregate([
  {
    $match: {}
  },
  {
    $sort: {
      CreatedAt: -1
    }
  },
  {
    $group: {
      _id: "$RateKey",
      hotel: {
        $first: "$$ROOT"
      }
    }
  },
  {
    "$replaceWith": "$hotel"
  }
])

Sample Mongo Playground


NET驱动程序C#语法

public class GroupedHotelResult
{
    [BsonId]
    public string Id { get; set; }

    [BsonElement("hotel")]
    public HotelResultDocument Hotel { get; set; }
}
SortDefinition<HotelResultDocument> sort =
    Builders<HotelResultDocument>.Sort.Descending(x => x.CreatedAt);

ProjectionDefinition<HotelResultDocument, GroupedHotelResult> group =
    new BsonDocument
    {
        { "_id", "$RateKey" },
        { "hotel", new BsonDocument
            {
                { "$first", "$$ROOT" }
            }
        }
    };

return await _collection
    .Aggregate()
    .Match(filter)
    .Sort(sort)
    .Group<GroupedHotelResult>(group)
    .ReplaceWith<GroupedHotelResult, HotelResultDocument>(x => x.Hotel)
    .ToListAsync();

输出

enter image description here

Csharp相关问答推荐

实体框架核心上是否支持使用NPGSQL的字符串聚合?

使页面内容居中

模型绑定RazorPage表单

Amazon SP-API确认发货不设置&Quot;递送服务

try 使用C#ASP.NET收集WMI信息时访问被拒绝,但在PowerShell中工作

取决于您的数据量的多个嵌套循环

C#中浮点数的System.Text.Json序列化问题

EFR32BG22 BLE在SPP模式下与PC(Windows 10)不连接

HttpClient SendAsync与多线程环境中的ArchiveZip

MSI无法将快捷方式添加到启动文件夹

在等待OnGetAsync时打开Razor Page显示微调器

用于请求用户返回列表的C#Google API

ASP.NET MVC数据批注验证组复选框

仅在ASP.NETCore应用程序中的附加单独端口上公开一组终结点

使用C#12中的主构造函数进行空判断

缩写的MonthNames有问题

是否在异步方法中避免Span<;T>;.ToArray()?

在SQL中删除少于24小时的令牌

带自定义比较器的OrderBy执行速度比默认比较器慢得多

合并3 IEnumerable<;应用程序>;将列表合并为一