问题描述
我上了这个课:
[BsonCollection("alerts")]
public class Alert : Document
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string AlertTypeId { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string MetricId { get; set; }
public ICollection<LabelValues> LabelsValues { get; set; }
public AlertType AlertType { get; set; }
public Metric Metric { get; set; }
public ICollection<Label> Labels { get; set; }
}
Element Alert具有“ LabelsValues”列表,其中每个项目均包含标签ID(指Labels集合的元素)及其值(标识符)的 some :
public class LabelValues
{
[BsonRepresentation(BsonType.ObjectId)]
public string LabelId { get; set; }
public ICollection<string> Values { get; set; }
}
我要做的是从数据库中检索所有加载了参考的警报(AlertType,Metric和Labels)。
现在我正在这样做:
var aggregation = collection.Aggregate().Match(filterDeFinition);
return aggregation
.Lookup<Alert,AlertType>(foreignCollectionName: "alerts",localField: "AlertTypeId",foreignField: "Id",@as: "AlertType")
.Unwind("AlertType")
.Lookup<Alert,Metric>(foreignCollectionName: "metrics",localField: "MetricId",@as: "Metric")
.Unwind("Metric")
.Lookup<Alert,Label>(foreignCollectionName: "labels",localField: "LabelsValues.LabelId",@as: "Labels")
.As<Alert>();
在检索AlertType,Metric和Labels并将它们设置为Alert中各自的属性方面,这是非常有用的。
但是我也想将值填充到“ Label”元素中,仅填充LabelsValues中包含的那些元素(而不是特定Label在数据库中提供的所有值)。
[BsonCollection("labels")]
public class Label
{
public ObjectId Id { get; set; }
public string NameIdentifier { get; set; }
public string displayName { get; set; }
public IList<Value> Values { get; set; } //--> This is the one I want to populate filtered
}
Ang的“值”为:
public class Value
{
public string NameIdentifier { get; set; }
public string displayName { get; set; }
}
现在,可以很好地检索标签,但是它们将数据库中的所有值(对于每个标签,我想按Alert中“ LabelsValues”中包含的值进行过滤)进行过滤。
我知道以后可以这样做,并且效果很好:
foreach (var alert in aggregation.ToList())
{
alert.Labels.ForEach(label => label.Values = label.Values.Where(v => alert.LabelsValues.Single(lv => lv.LabelId == label.Id.ToString()).Values.Contains(v.NameIdentifier)).ToList());
}
但是能够执行此筛选器并在聚合查找过程中填充“值”,并且仅执行一个完整的查询请求,真是令人惊讶。
我是MongoDB驱动程序的新手,所以我不知道是否有办法。
任何帮助将不胜感激!谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)