需要更新嵌套数组MongodB C#的最后记录
我想更新嵌套数组的最后一个对象。这是我的数据库结构。
{
_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的最好的是更新实际的最后一项,而不仅仅是与最后一项匹配的第一项,
复制数组的最后一项
更改复制的属性
复制原始数组,而无需最后一项
将复制的项目添加到复制的数组
合并回集合
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:
Copy the last item of the array
Change the properties of the copy
Copy the original array without the last item
Add the copied item to the copied array
Merge back to the 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.