如何使用Mvc内核在mongodb中插入嵌入式对象
我试图在现有Monogodb文档中插入、更新和删除(CRUD)一个嵌入对象, 假设为客户添加多个地址如何使用Mvc内核在mongodb中插入嵌入式对象,mongodb,asp.net-core,model-view-controller,nosql,crud,Mongodb,Asp.net Core,Model View Controller,Nosql,Crud,我试图在现有Monogodb文档中插入、更新和删除(CRUD)一个嵌入对象, 假设为客户添加多个地址 { "_id":ObjectId("52ffc33cd85242f436000001"), "contact": "987654321", "dob": "01-01-1991", "name": "Tom Benzamin", "address": [ { "building": "22 A, Indiana Apt",
{
"_id":ObjectId("52ffc33cd85242f436000001"),
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin",
"address": [
{
"building": "22 A, Indiana Apt",
"pincode": 123456,
"city": "Los Angeles",
"state": "California"
},
{
"building": "170 A, Acropolis Apt",
"pincode": 456789,
"city": "Chicago",
"state": "Illinois"
}
]
}
我可以使用命令行(添加多个地址),但通过mvc core,我只能在使用InserOne插入文档时插入单个地址,也可以使用ReplaceOne进行更新,但我可以添加第二个或第三个地址
这是我的c#类和接口(服务)
服务呢
public Customer Create(Customer c)
{
customers.InsertOne(c);
return c;
}
public void Update(string id, Customer c)
{
customers.ReplaceOne(cutomer => cutomer.Id == id, c);
}
请包括您的代码。我假设它是C#?@barrypicker,我已经添加了这个类,有什么想法吗?2个问题-首先,属性“Id”应该是“u Id”。其次,属性“Address”作为类型“Addresses”,而类“Addresses”不是列表。请改为尝试
公共列表地址{get;set;}
。@barrypicker为什么我要将它们定义为列表?我可以通过mongodb命令添加address对象,没有任何问题。我想应该是一样的。@ahmed-您的示例JSON显示了一个地址数组,因此C#model的对应类型应该是List。
public Customer Create(Customer c)
{
customers.InsertOne(c);
return c;
}
public void Update(string id, Customer c)
{
customers.ReplaceOne(cutomer => cutomer.Id == id, c);
}