使用 C# 驱动程序在嵌套 mongo 集合中添加新对象
Adding a new object in a nested mongo collection using C# drivers
我想使用 C# drivers
在嵌套的 Mongo
集合中添加一个新对象,请在下面找到示例 json。
我有一个名为 students 的集合,与下面类似。
{
"_id" : ObjectId("5a866be6f843b648a0bac9ab"),
"FIstName" : "Bob",
"LastName" : "mark",
"MarkList" : [
{
"SUbject" : "science",
"Mark" : "80",
"Rank" : "10",
}
],
}
现在我想使用 C# 驱动程序在 Marklist 数组下插入一个新的主题对象,
{
"SUbject" : "Physics",
"Mark" : "80",
"Rank" : "10",
}
我还为上述属性创建了 类。
请提出一些想法。
提前致谢
最简单的方法是使用 $push
operator but $addToSet
也可以,具体取决于您的要求。
如果我们在 Mongo Shell 中构造它,您将得到类似
的内容
db.students.update({"_id": ObjectId("5a866be6f843b648a0bac9ab")},
{
$push: {
"MarkList": {
"Subject": "Physics",
"Mark": 80,
"Rank": 10
}
}
})
所以将其转换为 C#,假设您已经掌握了一些东西。
- 一个名为
studentsCol
的 IMongoCollection<T>
对象
- 该集合中
T
的类型是 Student
- 学生的Id存储在变量
studentId
- 要插入的新对象存储在变量中
newMark
在这种情况下,以下代码将完成您要查找的内容。
studentsCol.UpdateOneAsync(
Builders<Student>.Filter.Eq(x => x.Id, studentId),
Builders<Student>.Update.Push(x => x.MarkList, newMark));
如果你想使用 $addToSet
,你只需将 Update.Push
更改为 Update.AddToSet
。
注意:如果您选择 $addToSet
,请务必阅读 MongoDB 如何确定数组中是否已存在文档。
If the value is a document, MongoDB determines that the document is a duplicate if an existing document in the array matches the to-be-added document exactly; i.e. the existing document has the exact same fields and values and the fields are in the same order. As such, field order matters and you cannot specify that MongoDB compare only a subset of the fields in the document to determine whether the document is a duplicate of an existing array element.
我想使用 C# drivers
在嵌套的 Mongo
集合中添加一个新对象,请在下面找到示例 json。
我有一个名为 students 的集合,与下面类似。
{
"_id" : ObjectId("5a866be6f843b648a0bac9ab"),
"FIstName" : "Bob",
"LastName" : "mark",
"MarkList" : [
{
"SUbject" : "science",
"Mark" : "80",
"Rank" : "10",
}
],
}
现在我想使用 C# 驱动程序在 Marklist 数组下插入一个新的主题对象,
{
"SUbject" : "Physics",
"Mark" : "80",
"Rank" : "10",
}
我还为上述属性创建了 类。
请提出一些想法。
提前致谢
最简单的方法是使用 $push
operator but $addToSet
也可以,具体取决于您的要求。
如果我们在 Mongo Shell 中构造它,您将得到类似
的内容db.students.update({"_id": ObjectId("5a866be6f843b648a0bac9ab")},
{
$push: {
"MarkList": {
"Subject": "Physics",
"Mark": 80,
"Rank": 10
}
}
})
所以将其转换为 C#,假设您已经掌握了一些东西。
- 一个名为
studentsCol
的 - 该集合中
T
的类型是Student
- 学生的Id存储在变量
studentId
- 要插入的新对象存储在变量中
newMark
IMongoCollection<T>
对象
在这种情况下,以下代码将完成您要查找的内容。
studentsCol.UpdateOneAsync(
Builders<Student>.Filter.Eq(x => x.Id, studentId),
Builders<Student>.Update.Push(x => x.MarkList, newMark));
如果你想使用 $addToSet
,你只需将 Update.Push
更改为 Update.AddToSet
。
注意:如果您选择 $addToSet
,请务必阅读 MongoDB 如何确定数组中是否已存在文档。
If the value is a document, MongoDB determines that the document is a duplicate if an existing document in the array matches the to-be-added document exactly; i.e. the existing document has the exact same fields and values and the fields are in the same order. As such, field order matters and you cannot specify that MongoDB compare only a subset of the fields in the document to determine whether the document is a duplicate of an existing array element.