如何正确格式化此MongoDB更新子句?

时间:2015-08-31 00:31:13

标签: c# mongodb insert-update

我有以下文件:

{
"_id": {
    "$oid": "55e1f841ff149c2228a5c33d"
},
"Status": "Open",
"Date": "8/30/2015",
"ContestName": "Test Contest",
"SearchableContestName": "test contest",
"ClassName": "Test",
"SearchableClassName": "test",
"Judges": [
    {
        "Name": "First Last",
        "IsHeadJudge": null,
        "_id": {
            "$oid": "55e20962ff149c1f70d1aab0"
        },
        "ContestScores": null
    },
    {
        "Name": "Another Name",
        "IsHeadJudge": null,
        "_id": {
            "$oid": "55e20947ff149c1f70d1aaaf"
        },
        "ContestScores": [
            1,
            2,
            3,
            4,
            5,
            6
        ]
    },

...

此列表中有多名评委。我想有选择地仅更新“最后一个”姓名个人的比赛分数。我很难找到为此构建我的过滤器的正确方法。我有:

 public async void UpdateContestScores(ContestJudge judgeData, ObjectId contestId)
    {
        var contests = _db.GetCollection<BsonDocument>("contests");
        var builder = Builders<BsonDocument>.Filter;
        var updateFilter = builder.Eq("_id", contestId) & builder.Eq("Judges.Name", judgeData.Name);

        var update = Builders<BsonDocument>.Update.Set("Judges.ContestScores", judgeData.ContestScores);

        await contests.UpdateOneAsync(updateFilter, update);
    }

这会抛出某种形式的批量更新错误。如何更新此空竞赛分数字段?我不确定这种东西的正确过滤器语法。

0 个答案:

没有答案