需要更新嵌套数组MongodB C#的最后记录

发布于 2025-01-28 18:06:12 字数 1423 浏览 3 评论 0原文

我想更新嵌套数组的最后一个对象。这是我的数据库结构。

{
  _id:1,
  isActive:true,
  name:xyz,
  student:[
    {
      id:12,
      name:"a",
      dateCreated: "1-1-2022 :20:08:00",
    },
    {
      id:12,
      name:"a",
      dateCreated: "1-1-2022 :22:10:00",
    }
]

我会遇到一个问题,即我在嵌套对象的记录中都有记录。 我都有两个对象的细节,但我想更新学生的最后一个对象。我正在使用以下方法,该方法更新了我的第一个学生对象。我找不到解决方案。如果有人有主意,请告诉我。 预先感谢

//过滤器

var filter = Builders<UpdateFrameDto>.Filter.Eq(x => x.IsActive, true);

filter &= Builders<UpdateFrameDto>.Filter.ElemMatch(_ => _.student, _ => _.id== 12);

var bBuilder = Builders<UpdateFrameDto>.Update.Set(x => x.student[-1].name, "noman"));
 
var result = _updateFrameCollection.UpdateMany(filter, bBuilder);

当前输出

{
  _id:1,
  isActive:true,
  name:xyz,
  student:[
    {
      id:12,
      name:"noman",
      dateCreated: "1-1-2022 :20:08:00",
    },// updating this record
    {
      id:12,
      name:"a",
      dateCreated: "1-1-2022 :22:10:00",
    }
  ]
}

预期输出

{
  _id:1,
  isActive:true,
  name:xyz,
  student:[
    {
      id:12,
      name:"a",
      dateCreated: "1-1-2022 :20:08:00",
    },
    {
      id:12,
      name:"noman",
      dateCreated: "1-1-2022 :22:10:00",
    } // want to update this record.
]

I want to update the last object of nested array. Here is my DB structure.

{
  _id:1,
  isActive:true,
  name:xyz,
  student:[
    {
      id:12,
      name:"a",
      dateCreated: "1-1-2022 :20:08:00",
    },
    {
      id:12,
      name:"a",
      dateCreated: "1-1-2022 :22:10:00",
    }
]

I'm getting a problem that i have a record with nested array of objects. I have the same detail of both objects but i want to update the last object of student. I'm using the below approach which updates my first object of student. I'm unable to find the solution. If anyone have idea let me know.
Thanks in advance

// filter

var filter = Builders<UpdateFrameDto>.Filter.Eq(x => x.IsActive, true);

filter &= Builders<UpdateFrameDto>.Filter.ElemMatch(_ => _.student, _ => _.id== 12);

var bBuilder = Builders<UpdateFrameDto>.Update.Set(x => x.student[-1].name, "noman"));
 
var result = _updateFrameCollection.UpdateMany(filter, bBuilder);

Current output

{
  _id:1,
  isActive:true,
  name:xyz,
  student:[
    {
      id:12,
      name:"noman",
      dateCreated: "1-1-2022 :20:08:00",
    },// updating this record
    {
      id:12,
      name:"a",
      dateCreated: "1-1-2022 :22:10:00",
    }
  ]
}

Expected output

{
  _id:1,
  isActive:true,
  name:xyz,
  student:[
    {
      id:12,
      name:"a",
      dateCreated: "1-1-2022 :20:08:00",
    },
    {
      id:12,
      name:"noman",
      dateCreated: "1-1-2022 :22:10:00",
    } // want to update this record.
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2025-02-04 18:06:12

我能想到的最好的是更新实际的最后一项,而不仅仅是与最后一项匹配的第一项,

  1. 复制数组的最后一项

  2. 更改复制的属性

  3. 复制原始数组,而无需最后一项

  4. 将复制的项目添加到复制的数组

  5. 合并回集合

db.collection.aggregate([
  {
    $addFields: {lastStudent: {$last: "$student"}}
  },
  { 
    $set: {"lastStudent.name": "noman", count: {$subtract: [{$size: "$student"}, 1]}}
  },
  {
    $addFields: {
      reducedStudent: {$slice: ["$student", "$count" ]}
    }
  },
  {
    $project: {
      isActive: 1,
      name: 1,
      student: {$concatArrays: ["$reducedStudent", ["$lastStudent"]]}
    }
  },
  {
    $merge: {
      into: "collection"
    }
  }
])

Playground示例

有很多方法可以更新匹配的第一个项目,但是实际的最后一项(包括数组中的重复项)并不是那么直接。

The best I could think of to update the actual last item and not just the first item that matches the last item, is:

  1. Copy the last item of the array

  2. Change the properties of the copy

  3. Copy the original array without the last item

  4. Add the copied item to the copied array

  5. Merge back to the collection

db.collection.aggregate([
  {
    $addFields: {lastStudent: {$last: "$student"}}
  },
  { 
    $set: {"lastStudent.name": "noman", count: {$subtract: [{$size: "$student"}, 1]}}
  },
  {
    $addFields: {
      reducedStudent: {$slice: ["$student", "$count" ]}
    }
  },
  {
    $project: {
      isActive: 1,
      name: 1,
      student: {$concatArrays: ["$reducedStudent", ["$lastStudent"]]}
    }
  },
  {
    $merge: {
      into: "collection"
    }
  }
])

playground example.

There are many ways to update the first item that matches, but the actual last item (including the case of duplicates inside the array), is not that straight-forward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文