Mongodb c# 适配器 2.2 查询 - 具有过滤条件和有限列的前 10 个排序结果
Mongodb c# adapter 2.2 query - Top 10 sorted result with filter condition and limited columns
下面是mongodb中的BSON数据格式。集合中有 300000 个文档。
{
"_id" : "90D6F6B3-B4ED-4665-B3E2-09733F340367",
"RootID" : "B607F413-E248-430A-910A-304711BA4BD9",
"SubRootId" : "C67F493E-82CB-474B-A3B8-B8769E0878E9",
"SubRootVersion" : NumberInt(1),
"Orders" : [
NumberInt(247),
NumberInt(205),
NumberInt(279),
NumberInt(272),
NumberInt(256),
NumberInt(236),
NumberInt(222),
NumberInt(250),
NumberInt(287),
NumberInt(261),
NumberInt(281)
],
"SubRootUpdatedDate" : ISODate("2016-06-20T17:15:14.595+0000"),
"UniqueNumber" : NumberLong(15410)
}
{
"_id" : "09ECF9DE-B26F-49FD-B6BD-61B3A2FF4EF4",
"RootID" : "B607F413-E248-430A-910A-304711BA4BD9",
"SubRootId" : "C67F493E-82CB-474B-A3B8-B8769E0878E9",
"SubRootVersion" : NumberInt(1),
"Orders" : [
NumberInt(247),
NumberInt(205),
NumberInt(279),
NumberInt(272),
NumberInt(256),
NumberInt(236),
NumberInt(222),
NumberInt(250),
NumberInt(287),
NumberInt(261),
NumberInt(281)
],
"SubRootUpdatedDate" : ISODate("2016-06-20T17:15:14.609+0000"),
"UniqueNumber" : NumberLong(15411)
}
{
"_id" : "038DEB63-196D-4168-9BA8-FA011E14CD09",
"RootID" : "919C747A-A024-4615-AFD3-67F86BBD5360",
"SubRootId" : "DAEB7FE6-C4BC-4E75-B9AE-AFC1B9999F13",
"SubRootVersion" : NumberInt(2),
"Orders" : [
NumberInt(211),
NumberInt(203),
NumberInt(231),
NumberInt(260),
NumberInt(213),
NumberInt(203),
NumberInt(200),
NumberInt(213),
NumberInt(200),
NumberInt(233),
NumberInt(220),
NumberInt(230),
NumberInt(297),
NumberInt(256)
],
"SubRootUpdatedDate" : ISODate("2016-06-20T17:15:09.312+0000"),
"UniqueNumber" : NumberLong(15116)
}
{
"_id" : "BFCAC497-7206-4EBD-B2E9-F521A90536A5",
"RootID" : "919C747A-A024-4615-AFD3-67F86BBD5360",
"SubRootId" : "DAEB7FE6-C4BC-4E75-B9AE-AFC1B9999F13",
"SubRootVersion" : NumberInt(2),
"Orders" : [
NumberInt(211),
NumberInt(203),
NumberInt(231),
NumberInt(260),
NumberInt(213),
NumberInt(203),
NumberInt(200),
NumberInt(213),
NumberInt(200),
NumberInt(233),
NumberInt(220),
NumberInt(230),
NumberInt(297),
NumberInt(256)
],
"SubRootUpdatedDate" : ISODate("2016-06-20T17:15:09.347+0000"),
"UniqueNumber" : NumberLong(15117)
}
只有sequencenumber和filingid是唯一的。
我正在使用最新的 c# 2.2 驱动程序来查询以下条件。
1) 前 10 个最新的 RootId 记录(最新基于 SubRootUpdatedDate )
2) 匹配订单列表的记录
3) 在 SubRootUpdatedDate 日期范围
之间
这是我试过的
var collection = _database.GetCollection<SubRoot>("SubRoots");
var sort = Builders<SubRoot>.Sort.Descending(x => x.UniqueNumber);
var sortByDate = Builders<SubRoot>.Sort.Descending(x => x.SubRootUpdatedDate);
var filter = Builders<SubRoot>.Filter.In("Orders", new list<int> { 255, 256 , 299, 281});
filter &= Builders<SubRoot>.Filter.Gt(y => y.SubRootUpdatedDate, minDate);
filter &= Builders<SubRoot>.Filter.Lt(y => y.SubRootUpdatedDate, maxDate);
var projection = Builders<SubRoot>.Projection
.Exclude(x => x.SubRootUpdatedDate)
.Exclude(x => x.SubRootId)
.Exclude(x => x.UniqueNumber)
.Exclude(x => x._id)
.Exclude(x => x.Orders)
.Exclude(x => x.SubRootVersion);
return collection.Find(filter).Sort(sort).Sort(sortByDate).Project(projection).ToList();
我在使用过滤和排序后无法进行区分。此外,在执行 Tolist() 之前,我无法将结果限制为 10。我希望在 mongodb 中完成所有的数据过滤、排序和限制。我只想带回符合搜索条件的 10 个 ID。我已经花了足够的时间,如果有任何建议,我将不胜感激。
我自己找到了答案。
var aggregate = collection.Aggregate()
.Match(Builders<Filing>.Filter.In("Orders", ordersList))
.Match(x => x.UniqueNumber>= UniqueNumberList)
.Match(Builders<Filing>.Filter.Gt(y => y.SubRootUpdatedDate, minDate))
.Match(Builders<Filing>.Filter.Lt(y => y.SubRootUpdatedDate, maxDate))
.Sort(Builders<Filing>.Sort.Descending(x => x.UniqueNumber))
.Sort(Builders<Filing>.Sort.Descending(x => x.SubRootUpdatedDate))
.Group(z => z.RootID, h => new { h.Key })
.Limit(limitValue)
.ToList();
下面是mongodb中的BSON数据格式。集合中有 300000 个文档。
{
"_id" : "90D6F6B3-B4ED-4665-B3E2-09733F340367",
"RootID" : "B607F413-E248-430A-910A-304711BA4BD9",
"SubRootId" : "C67F493E-82CB-474B-A3B8-B8769E0878E9",
"SubRootVersion" : NumberInt(1),
"Orders" : [
NumberInt(247),
NumberInt(205),
NumberInt(279),
NumberInt(272),
NumberInt(256),
NumberInt(236),
NumberInt(222),
NumberInt(250),
NumberInt(287),
NumberInt(261),
NumberInt(281)
],
"SubRootUpdatedDate" : ISODate("2016-06-20T17:15:14.595+0000"),
"UniqueNumber" : NumberLong(15410)
}
{
"_id" : "09ECF9DE-B26F-49FD-B6BD-61B3A2FF4EF4",
"RootID" : "B607F413-E248-430A-910A-304711BA4BD9",
"SubRootId" : "C67F493E-82CB-474B-A3B8-B8769E0878E9",
"SubRootVersion" : NumberInt(1),
"Orders" : [
NumberInt(247),
NumberInt(205),
NumberInt(279),
NumberInt(272),
NumberInt(256),
NumberInt(236),
NumberInt(222),
NumberInt(250),
NumberInt(287),
NumberInt(261),
NumberInt(281)
],
"SubRootUpdatedDate" : ISODate("2016-06-20T17:15:14.609+0000"),
"UniqueNumber" : NumberLong(15411)
}
{
"_id" : "038DEB63-196D-4168-9BA8-FA011E14CD09",
"RootID" : "919C747A-A024-4615-AFD3-67F86BBD5360",
"SubRootId" : "DAEB7FE6-C4BC-4E75-B9AE-AFC1B9999F13",
"SubRootVersion" : NumberInt(2),
"Orders" : [
NumberInt(211),
NumberInt(203),
NumberInt(231),
NumberInt(260),
NumberInt(213),
NumberInt(203),
NumberInt(200),
NumberInt(213),
NumberInt(200),
NumberInt(233),
NumberInt(220),
NumberInt(230),
NumberInt(297),
NumberInt(256)
],
"SubRootUpdatedDate" : ISODate("2016-06-20T17:15:09.312+0000"),
"UniqueNumber" : NumberLong(15116)
}
{
"_id" : "BFCAC497-7206-4EBD-B2E9-F521A90536A5",
"RootID" : "919C747A-A024-4615-AFD3-67F86BBD5360",
"SubRootId" : "DAEB7FE6-C4BC-4E75-B9AE-AFC1B9999F13",
"SubRootVersion" : NumberInt(2),
"Orders" : [
NumberInt(211),
NumberInt(203),
NumberInt(231),
NumberInt(260),
NumberInt(213),
NumberInt(203),
NumberInt(200),
NumberInt(213),
NumberInt(200),
NumberInt(233),
NumberInt(220),
NumberInt(230),
NumberInt(297),
NumberInt(256)
],
"SubRootUpdatedDate" : ISODate("2016-06-20T17:15:09.347+0000"),
"UniqueNumber" : NumberLong(15117)
}
只有sequencenumber和filingid是唯一的。
我正在使用最新的 c# 2.2 驱动程序来查询以下条件。
1) 前 10 个最新的 RootId 记录(最新基于 SubRootUpdatedDate ) 2) 匹配订单列表的记录 3) 在 SubRootUpdatedDate 日期范围
之间这是我试过的
var collection = _database.GetCollection<SubRoot>("SubRoots");
var sort = Builders<SubRoot>.Sort.Descending(x => x.UniqueNumber);
var sortByDate = Builders<SubRoot>.Sort.Descending(x => x.SubRootUpdatedDate);
var filter = Builders<SubRoot>.Filter.In("Orders", new list<int> { 255, 256 , 299, 281});
filter &= Builders<SubRoot>.Filter.Gt(y => y.SubRootUpdatedDate, minDate);
filter &= Builders<SubRoot>.Filter.Lt(y => y.SubRootUpdatedDate, maxDate);
var projection = Builders<SubRoot>.Projection
.Exclude(x => x.SubRootUpdatedDate)
.Exclude(x => x.SubRootId)
.Exclude(x => x.UniqueNumber)
.Exclude(x => x._id)
.Exclude(x => x.Orders)
.Exclude(x => x.SubRootVersion);
return collection.Find(filter).Sort(sort).Sort(sortByDate).Project(projection).ToList();
我在使用过滤和排序后无法进行区分。此外,在执行 Tolist() 之前,我无法将结果限制为 10。我希望在 mongodb 中完成所有的数据过滤、排序和限制。我只想带回符合搜索条件的 10 个 ID。我已经花了足够的时间,如果有任何建议,我将不胜感激。
我自己找到了答案。
var aggregate = collection.Aggregate()
.Match(Builders<Filing>.Filter.In("Orders", ordersList))
.Match(x => x.UniqueNumber>= UniqueNumberList)
.Match(Builders<Filing>.Filter.Gt(y => y.SubRootUpdatedDate, minDate))
.Match(Builders<Filing>.Filter.Lt(y => y.SubRootUpdatedDate, maxDate))
.Sort(Builders<Filing>.Sort.Descending(x => x.UniqueNumber))
.Sort(Builders<Filing>.Sort.Descending(x => x.SubRootUpdatedDate))
.Group(z => z.RootID, h => new { h.Key })
.Limit(limitValue)
.ToList();