MongoDb 更新嵌套数组项
MongoDb update nested array item
我有以下数据库结构。
{
"classes": [
{
"_id": "abcd1234",
"classname": "11/A",
"students": [
{
"_id": "student1",
"firstname": "Don",
"lastname": "Joe"
},
{
"_id": "student2",
"firstname": "Doo",
"lastname": "Joe"
},
...
]
},
...
]
}
如何使用 C# MongoDb Driver
更新学生的 ID?
像这样:
public async Task UpdateStudent(string studentId, Student student)
{
// implementation???
}
您可以使用 MongoDB 位置运算符 ($),它在 C# 中只是数组中 -1
的索引。
var filter = Builders<Class>.Filter.ElemMatch(x => x.Students, Builders<Student>.Filter.Eq(x => x.Id, "student2"));
var update = Builders<Class>.Update.Set(x => x.Students[-1].Firstname, "NEW FIRST NAME")
.Set(x => x.Students[-1].Lastname, "NEW LAST NAME");
await collection.UpdateOneAsync(filter, update);
public class Class
{
public string Id { get; set; }
public string Classname { get; set; }
public Student[] Students { get; set; }
}
public class Student
{
public string Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
例如,如果您像下面这样将一些文档推送到我们的集合中
var client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<Class>("classes");
await collection.InsertManyAsync(new[]
{
new Class
{
Id= Guid.NewGuid().ToString(),Classname = "Class 1", Students = new Student[]
{
new Student{Id = "student1", Firstname = "Don", Lastname = "Joe"},
new Student{Id = "student2", Firstname = "Doo", Lastname = "Joe"}
}
},
new Class
{
Id= Guid.NewGuid().ToString(),Classname = "Class 1", Students = new Student[]
{
new Student{Id = "student3", Firstname = "Don2", Lastname = "Joe2"},
new Student{Id = "student4", Firstname = "Doo2", Lastname = "Joe2"}
}
}
});
如果我们 运行 上面的更新我们的数据将在 shell
中看起来像下面这样
> db.classes.find().pretty()
{
"_id" : "b0413433-199a-4df0-bef0-0c6ae13e08b9",
"Classname" : "Class 1",
"Students" : [
{
"_id" : "student1",
"Firstname" : "Don",
"Lastname" : "Joe"
},
{
"_id" : "student2",
"Firstname" : "NEW FIRST NAME",
"Lastname" : "NEW LAST NAME"
}
]
}
{
"_id" : "b88c2725-3046-4717-96f2-e0553a5d3c96",
"Classname" : "Class 1",
"Students" : [
{
"_id" : "student3",
"Firstname" : "Don2",
"Lastname" : "Joe2"
},
{
"_id" : "student4",
"Firstname" : "Doo2",
"Lastname" : "Joe2"
}
]
}
>
查看以下内容post:https://kevsoft.net/2020/03/23/updating-arrays-in-mongodb-with-csharp.html
我有以下数据库结构。
{
"classes": [
{
"_id": "abcd1234",
"classname": "11/A",
"students": [
{
"_id": "student1",
"firstname": "Don",
"lastname": "Joe"
},
{
"_id": "student2",
"firstname": "Doo",
"lastname": "Joe"
},
...
]
},
...
]
}
如何使用 C# MongoDb Driver
更新学生的 ID?
像这样:
public async Task UpdateStudent(string studentId, Student student)
{
// implementation???
}
您可以使用 MongoDB 位置运算符 ($),它在 C# 中只是数组中 -1
的索引。
var filter = Builders<Class>.Filter.ElemMatch(x => x.Students, Builders<Student>.Filter.Eq(x => x.Id, "student2"));
var update = Builders<Class>.Update.Set(x => x.Students[-1].Firstname, "NEW FIRST NAME")
.Set(x => x.Students[-1].Lastname, "NEW LAST NAME");
await collection.UpdateOneAsync(filter, update);
public class Class
{
public string Id { get; set; }
public string Classname { get; set; }
public Student[] Students { get; set; }
}
public class Student
{
public string Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
例如,如果您像下面这样将一些文档推送到我们的集合中
var client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<Class>("classes");
await collection.InsertManyAsync(new[]
{
new Class
{
Id= Guid.NewGuid().ToString(),Classname = "Class 1", Students = new Student[]
{
new Student{Id = "student1", Firstname = "Don", Lastname = "Joe"},
new Student{Id = "student2", Firstname = "Doo", Lastname = "Joe"}
}
},
new Class
{
Id= Guid.NewGuid().ToString(),Classname = "Class 1", Students = new Student[]
{
new Student{Id = "student3", Firstname = "Don2", Lastname = "Joe2"},
new Student{Id = "student4", Firstname = "Doo2", Lastname = "Joe2"}
}
}
});
如果我们 运行 上面的更新我们的数据将在 shell
中看起来像下面这样> db.classes.find().pretty()
{
"_id" : "b0413433-199a-4df0-bef0-0c6ae13e08b9",
"Classname" : "Class 1",
"Students" : [
{
"_id" : "student1",
"Firstname" : "Don",
"Lastname" : "Joe"
},
{
"_id" : "student2",
"Firstname" : "NEW FIRST NAME",
"Lastname" : "NEW LAST NAME"
}
]
}
{
"_id" : "b88c2725-3046-4717-96f2-e0553a5d3c96",
"Classname" : "Class 1",
"Students" : [
{
"_id" : "student3",
"Firstname" : "Don2",
"Lastname" : "Joe2"
},
{
"_id" : "student4",
"Firstname" : "Doo2",
"Lastname" : "Joe2"
}
]
}
>
查看以下内容post:https://kevsoft.net/2020/03/23/updating-arrays-in-mongodb-with-csharp.html