MongoDB .Net Driver update Cannot use the part ... to traverse the element

MongoDB .Net Driver update Cannot use the part ... to traverse the element

我正在尝试重命名字段,但最终出现错误。我可能缺少什么?

A write operation resulted in an error. cannot use the part (Orders of Orders.Number) to traverse the element ({Orders: [ { CustomFields: { Пара паков: { FieldId: ObjectId('61e670f9c0fe1106a51a2e23'), Type: "Text", ValueBson: "8989" }, Weight: { FieldId: ObjectId('6149d5de44175b8324482915'), Type: "Number", ValueBson: 2222 } }, _id: ObjectId('61e6751027c21629e070091b'), Number: "21312" } ]})

var orderFilter = Builders<VisitTask>.Filter.Empty;
UpdateDefinition<VisitTask> update = Builders<VisitTask>.Update.Rename($"Orders.Number", $"Orders.TestNumber");
Collection.UpdateMany(orderFilter, update, new UpdateOptions { IsUpsert = true });

文档

{
    "_id" : ObjectId("61e674fa27c21629e070091a"),
    "LastUpdateDate" : ISODate("2022-01-18T08:07:52.008Z"),
    "Orders" : [ 
        {
            "CustomFields" : {
                "Пара паков" : {
                    "FieldId" : ObjectId("61e670f9c0fe1106a51a2e23"),
                    "Type" : "Text",
                    "ValueBson" : "8989"
                }
            },
            "_id" : ObjectId("61e6751027c21629e070091b"),
            "Number" : "21312"
        }
    ],
}

$rename 运算符无法更新嵌套数组中的字段名称。

相反,您需要 $set 将现有的 Number 值添加到具有 $map 的新字段 TestNumber。然后 $unset 删除 Orders.TestNumber 字段。

注意:之前的回答会导致输出不正确。需要使用 $map 来更新数组中的对象。

db.collection.update({},
[
  {
    $set: {
      Orders: {
        $map: {
          input: "$Orders",
          in: {
            $mergeObjects: [
              {
                "Number-test": "$$this.Number"
              },
              "$$this"
            ]
          }
        }
      }
    }
  },
  {
    $unset: "Orders.Number"
  }
],
{
  multi: true
})

Sample Mongo Playground


[对于 MongoDB .Net 驱动程序版本 2.13]

对于C#,你需要使用UpdateDefinitionBuilder<TDocument>.Pipeline Method如下:

解决方案 1

UpdateDefinition<VisitTask> update = Builders<VisitTask>.Update.Pipeline(
    new PipelineStagePipelineDefinition<VisitTask, VisitTask>(
        new PipelineStageDefinition<VisitTask, VisitTask>[]
        {
            new BsonDocument("$set", 
                new BsonDocument("Orders", 
                    new BsonDocument("$map", 
                        new BsonDocument
                        {
                            { "input", "$Orders" }, 
                            { "in", 
                                new BsonDocument("$mergeObjects", 
                                    new BsonArray
                                    {
                                        new BsonDocument("Number-test", "$$this.Number"),
                                        "$$this"
                                    }) 
                            }
                        }))),
            new BsonDocument("$unset", "Orders.Number")
        }));

解决方案 2

UpdateDefinition<VisitTask> update = Builders<VisitTask>.Update.Pipeline(
    new BsonDocument[]
    {
        new BsonDocument("$set", 
            new BsonDocument("Orders", 
                new BsonDocument("$map", 
                    new BsonDocument
                    {
                        { "input", "$Orders" }, 
                        { "in", 
                            new BsonDocument("$mergeObjects", 
                                new BsonArray
                                {
                                    new BsonDocument("Number-test", "$$this.Number"),
                                    "$$this"
                                }) 
                        }
                    }))),
        new BsonDocument("$unset", "Orders.Number")
    }
);