You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB聚合更新嵌套数组对象时如何正确获取当前文档字段值

Fixing the MongoDB Aggregation to Reference the Current Model's Name

The issue with your current code is that '$specs.name' refers to the name field of each element in the top-level specs array, not the name of the individual models entry you're modifying. To target the current model's name, you need to use nested $map stages to iterate through both the specs and models arrays, and reference the inner model variable within the valid subdocument.

Corrected C# Aggregation Code

var addNewFieldsDefinition = new BsonDocument
{
    { "$addFields", new BsonDocument
        {
            { "specs", new BsonDocument
                {
                    { "$map", new BsonDocument
                        {
                            { "input", "$specs" },
                            { "as", "spec" },
                            { "in", new BsonDocument
                                {
                                    { "name", "$$spec.name" },
                                    { "models", new BsonDocument
                                        {
                                            { "$map", new BsonDocument
                                                {
                                                    { "input", "$$spec.models" },
                                                    { "as", "model" },
                                                    { "in", new BsonDocument
                                                        {
                                                            { "$mergeObjects", new BsonArray
                                                                {
                                                                    // Omit the original "name" field from the model
                                                                    new BsonDocument("$omit", new BsonArray { "$$model", "name" }),
                                                                    // Add the new "valid" subdocument using the current model's name
                                                                    new BsonDocument
                                                                    {
                                                                        { "valid", new BsonDocument
                                                                            {
                                                                                { "isValid", true },
                                                                                { "currentName", new BsonArray { "$$model.name" } }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
};

var t1 = collection 
    .Aggregate() 
    .Match(filterDefinition) 
    .AppendStage<BsonDocument>(addNewFieldsDefinition) 
    .Merge(collection);

Key Changes Explained

  1. Nested $map Stages:

    • The outer $map iterates over each element in the specs array (using the variable $$spec).
    • The inner $map iterates over each model in the current spec's models array (using the variable $$model).
  2. Referencing the Current Model's Name:

    • $$model.name directly references the name of the individual model entry being processed, which is exactly what you need for currentName.
  3. Removing the Original name Field:

    • $omit removes the original name field from the model document, while preserving all other existing fields (like categories).
  4. Merging Fields:

    • $mergeObjects combines the modified model (without the name field) with the new valid subdocument, creating the desired structure for each model entry.

After running this aggregation, each models entry will have the valid subdocument with isValid: true and currentName set to the original model's name, and the original name field will be removed.

内容的提问来源于stack exchange,提问作者DreadAngel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:49:10