添加/阐述我最近的问题
以下是 DocumentDB 集合:“交付”
{
"doc": [
{
"docid": "15",
"deliverynum": "123",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
},
{
"docid": "17",
"deliverynum": "999",
"text": "txxxxxx",
"date": "2018-07-18T12:37:58Z"
}
],
"id": "123",
"cancelled": false
},
{
"doc": [
{
"docid": "16",
"deliverynum": "222",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
},
{
"docid": "17",
"deliverynum": "999",
"text": "txxxxxx",
"date": "2019-07-20T12:37:58Z"
}
],
"id": "124",
"cancelled": false
}
我需要搜索带有最新日期的 deliverynum=999 以获取“id”,在上述情况下为“124”,因为它在带有 deliverynum=999 的“文档”中具有最新的“日期”。
我打算这样做:
var list = await collection.Find(filter).Project(projection).ToListAsync();
然后做一个 LINQ 进行排序,但这里的问题是我的投影将列表从我的模型类更改为 BsonDocument,即使我的投影包含所有字段。
正在寻找一种方法来获取刚需要的“id”或获取单个文档。
我相信以下内容可以解决问题。(如果我正确理解您的要求)
var result = collection.Find(x => x.docs.Any(d => d.deliverynum == 999))
.Sort(Builders<Record>.Sort.Descending("docs.date"))
.Limit(1)
.Project(x=>x.Id) //remove this to get back the whole record
.ToList();
更新:强类型解决方案
var result = collection.AsQueryable()
.Where(r => r.docs.Any(d => d.deliverynum == 999))
.SelectMany(r => r.docs, (r, d) => new { r.Id, d.date })
.OrderByDescending(x => x.date)
.Take(1)
.Select(x => x.Id)
.ToArray();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句