MongoDB 和 LINQ: "NOT IN" 子句
MongoDB and LINQ: "NOT IN" clause
我有两个集合,一个是图像名称列表,第二个是该列表的子集。任务完成后,其名称将插入到第二个集合中。
我需要从第一个集合中检索一组尚未完成的图像名称。我通过以下方式成功实现了这一目标:
var processedNames = processed.AsQueryable().Select(x => x.ImageName).ToArray();
foreach (var result in results.Where(x => !processedNames.Contains(x.ImageName))
然而,这会从数据库中带回大量字符串,然后将其以单个文档的形式发送回数据库,这不仅效率低下,而且最终会崩溃。
所以我尝试重写它,以便它全部在服务器端执行:
var results = from x in captures
join complete in processed.AsQueryable() on x.ImageName equals complete.ImageName into completed
where !completed.Any()
select x;
这失败了:
System.NotSupportedException: '$project 或 $group 不支持 {document}。'
我也试过使用非 LINQ API:
var xs = capturesCollection.Aggregate()
.Lookup("Processed", "ImageName", "ImageName", @as: "CompletedCaptures")
.Match(x => x["CompletedCaptures"] == null)
.ToList();
这失败了:
MongoDB.Bson.BsonSerializationException: 'C# null values of type 'BsonValue' cannot be serialized using a serializer of type 'BsonValueSerializer'.'
如何使用 C# 驱动程序完全在服务器端实现此查询?纯 LINQ 解决方案更适合可移植性。
我弄清楚了如何使用 Aggregate
API:
var results = capturesCollection.Aggregate()
.As<CaptureWithCompletions>()
.Lookup(processed, x => x.ImageName, x => x.ImageName, @as:(CaptureWithCompletions x) => x.CompletedCaptures)
.Match(x => !x.CompletedCaptures.Any())
//.Limit(2)
.ToList();
我有两个集合,一个是图像名称列表,第二个是该列表的子集。任务完成后,其名称将插入到第二个集合中。
我需要从第一个集合中检索一组尚未完成的图像名称。我通过以下方式成功实现了这一目标:
var processedNames = processed.AsQueryable().Select(x => x.ImageName).ToArray();
foreach (var result in results.Where(x => !processedNames.Contains(x.ImageName))
然而,这会从数据库中带回大量字符串,然后将其以单个文档的形式发送回数据库,这不仅效率低下,而且最终会崩溃。
所以我尝试重写它,以便它全部在服务器端执行:
var results = from x in captures
join complete in processed.AsQueryable() on x.ImageName equals complete.ImageName into completed
where !completed.Any()
select x;
这失败了:
System.NotSupportedException: '$project 或 $group 不支持 {document}。'
我也试过使用非 LINQ API:
var xs = capturesCollection.Aggregate()
.Lookup("Processed", "ImageName", "ImageName", @as: "CompletedCaptures")
.Match(x => x["CompletedCaptures"] == null)
.ToList();
这失败了:
MongoDB.Bson.BsonSerializationException: 'C# null values of type 'BsonValue' cannot be serialized using a serializer of type 'BsonValueSerializer'.'
如何使用 C# 驱动程序完全在服务器端实现此查询?纯 LINQ 解决方案更适合可移植性。
我弄清楚了如何使用 Aggregate
API:
var results = capturesCollection.Aggregate()
.As<CaptureWithCompletions>()
.Lookup(processed, x => x.ImageName, x => x.ImageName, @as:(CaptureWithCompletions x) => x.CompletedCaptures)
.Match(x => !x.CompletedCaptures.Any())
//.Limit(2)
.ToList();