使用.Lookup() 进行聚合查询很热门吗? (MongoDB 驱动程序,c#)
Hot to make aggregate query with .Lookup()? (MongoDB driver, c#)
我是 MongoDB 驱动程序的新手,所以我对查询有点困惑。
我有 collections 和以下 类:
[BsonCollection("alerts")]
public class Alert
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string AlertTypeId { get; set; }
[BsonIgnore]
public AlertType AlertType { get; set; }
}
[BsonCollection("alert_types")]
public class AlertType
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
我正在尝试使用 Lookup 运算符在 Alert 中填充 属性 AlertType。
我尝试了以下方法:
var collection = database.GetCollection<Alert>(GetCollectionName(typeof(Alert)));
var query = collection.Aggregate()
.Lookup("alert_types", "AlertTypeId", "Id", "AlertType")
.Unwind("AlertType")
.As<Alert>()
.ToList();
但是这个 returns 是一个空列表。
我也这样试过:
var query = collection.Aggregate()
.Lookup("alert_types", x => ObjectId.Parse(x.AlertTypeId), y => y.Id, "AlertType")
.Unwind("AlertType")
.As<Alert>()
.ToList();
但我在查找标记中遇到错误:
"The type arguments for method
'IAggregateFluent.Lookup<TForeignDocument,
TNewResult>(string, FieldDefinition,
FieldDefinition, FieldDefinition,
AggregateLookupOptions<TForeignDocument, TNewResult>)' cannot be
inferred from the usage. Try specifying the type arguments
explicitly."
此外,尝试以这种方式将 collections 作为可查询的:
IEnumerable<Alert> query = from a in collection.AsQueryable()
join o in typesCollection.AsQueryable() on a.AlertTypeId equals o.Id.ToString() into joinedAlertTypes
select new Alert(a, joinedAlertTypes.FirstOrDefault());
(如您所见,在 Alert 中实现一个构造函数,该构造函数分配其所有属性以及连接中的 AlertType)
但是我得到错误:
Unable to determine the serialization information for the inner key
selector in the tree: aggregate([]).GroupJoin(aggregate([]), a =>
a.AlertTypeId, o => o.Id.ToString(), (a, joinedAlertTypes) => new
<>f__AnonymousType0`2(a = a, joinedAlertTypes = joinedAlertTypes))
我做错了什么?
The type arguments for method... cannot be inferred from the usage
您应该明确指定通用参数。
我是 MongoDB 驱动程序的新手,所以我对查询有点困惑。
我有 collections 和以下 类:
[BsonCollection("alerts")]
public class Alert
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string AlertTypeId { get; set; }
[BsonIgnore]
public AlertType AlertType { get; set; }
}
[BsonCollection("alert_types")]
public class AlertType
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
我正在尝试使用 Lookup 运算符在 Alert 中填充 属性 AlertType。 我尝试了以下方法:
var collection = database.GetCollection<Alert>(GetCollectionName(typeof(Alert)));
var query = collection.Aggregate()
.Lookup("alert_types", "AlertTypeId", "Id", "AlertType")
.Unwind("AlertType")
.As<Alert>()
.ToList();
但是这个 returns 是一个空列表。
我也这样试过:
var query = collection.Aggregate()
.Lookup("alert_types", x => ObjectId.Parse(x.AlertTypeId), y => y.Id, "AlertType")
.Unwind("AlertType")
.As<Alert>()
.ToList();
但我在查找标记中遇到错误:
"The type arguments for method 'IAggregateFluent.Lookup<TForeignDocument, TNewResult>(string, FieldDefinition, FieldDefinition, FieldDefinition, AggregateLookupOptions<TForeignDocument, TNewResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly."
此外,尝试以这种方式将 collections 作为可查询的:
IEnumerable<Alert> query = from a in collection.AsQueryable()
join o in typesCollection.AsQueryable() on a.AlertTypeId equals o.Id.ToString() into joinedAlertTypes
select new Alert(a, joinedAlertTypes.FirstOrDefault());
(如您所见,在 Alert 中实现一个构造函数,该构造函数分配其所有属性以及连接中的 AlertType)
但是我得到错误:
Unable to determine the serialization information for the inner key selector in the tree: aggregate([]).GroupJoin(aggregate([]), a => a.AlertTypeId, o => o.Id.ToString(), (a, joinedAlertTypes) => new <>f__AnonymousType0`2(a = a, joinedAlertTypes = joinedAlertTypes))
我做错了什么?
The type arguments for method... cannot be inferred from the usage
您应该明确指定通用参数。