我有这个问题:
var professionalProfilesQuery =
(from profileCollection in _mongoContext.Database.GetCollection<Profile>("profiles").AsQueryable()
join userCollection in _mongoContext.Database.GetCollection<User>("users").AsQueryable()
on profileCollection.UserId equals userCollection.Id.Value
into users
orderby profileCollection.Name
select new ProfessionalProfile
{
Id = profileCollection.Id,
Name = profileCollection.Name,
UserId = profileCollection.UserId,
IsConfirmed = users.First().IsConfirmed,
})
.Where(p => p.IsConfirmed);
然后我在DB中存储了一个javascript函数,根据几个集合中的数据计算每个ProfessionalProfile
的“分数”(我需要根据该分数对结果进行排序)。类似的东西:
function candidateScore(objectWithDataToCalculateScore, professionalProfile) {
var score = 0;
//Calculate score here
return score;
}
是否可以使用计算得分为查询返回的数据添加字段?如何调用存储的javascript函数呢?我想做点什么:
var professionalProfilesQuery =
(from profileCollection in _mongoContext.Database.GetCollection<Profile>("profiles").AsQueryable()
join userCollection in _mongoContext.Database.GetCollection<User>("users").AsQueryable()
on profileCollection.UserId equals userCollection.Id.Value
into users
orderby profileCollection.Name
select new ProfessionalProfile
{
Id = profileCollection.Id,
Name = profileCollection.Name,
UserId = profileCollection.UserId,
IsConfirmed = users.First().IsConfirmed,
Score = ***CallToCandidateScoreFunctionHere...***
})
.Where(p => p.IsConfirmed);
如果这不可行,或者这不是一个好方法,你会建议另一种方法而不是存储的javascript函数吗?
感谢。
编辑:在阅读Neil Lunn的评论后,我会更好地解释我需要的计算。
这是为前一个candidateScore
javascript函数中第一个参数的集合建模的类的示例:
public class ProfessionalProfileRequirements
{
[BsonId]
[BsonRepresentation(BsonType.String)]
public Guid Id { get; set; }
public string Role { get; set; }
public string Province { get; set; }
public IEnumerable<string> LanguagesRequired { get; set; }
public IEnumerable<string> SkillsRequired { get; set; }
}
这个javascript函数再次进行了一些计算(它非常复杂,所以我对它进行了简化。我希望它足以显示我需要的东西):
function candidateScore(professionalProfileRequirements, professionalProfile) {
var score = 0;
if (professionalProfileRequirements.Province === professionalProfile.Province) {
score += 1000;
}
if (professionalProfileRequirements.Role && professionalProfile.Roles && professionalProfile.ExperienceLevel){
if (professionalProfile.Roles.includes(professionalProfileRequirements.Role)) {
switch (professionalProfile.ExperienceLevel) {
case 'ExperienceLevel_NoExperience':
score += 1;
break;
case 'ExperienceLevel_LessThanOneYear':
score += 2;
break;
case 'ExperienceLevel_BetweenOneAndThreeYears':
score += 4;
break;
case 'ExperienceLevel_BetweenThreeAndFiveYears':
score += 6;
break;
case 'ExperienceLevel_MoreThan5Years':
score += 10;
break;
}
}
}
return score;
}
我不知道如何使用“聚合”进行这种计算。任何帮助或我可以遵循的任何其他方法将不胜感激。