如何使用C#.NET插入MongoDB中阵列中的数据? [英] How to insert data in mongodb array using C#.Net?
本文介绍了如何使用C#.NET插入MongoDB中阵列中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有Veh_info收集MongoDB中有
I have collection of Veh_info in mongodb having
id:1
color:red
Cly:4
Loc:{ Loc1
Loc2
}
我创建类,如
public class Car()
{ Public Objectid id {get;set;}
public color {get;set;}
public Cly {get;set;}
Public Location loc {get;set}
}
Public class Location()
{ public string loc{get;set;}
}
我不知道如何在数组中的C#的Click事件中插入新的记录即中Loc3。如果任何一个给一些hits.thanks
I have no idea how to insert new record i.e Loc3 in array in C# on Click event . If any one give some hits.thanks
推荐答案
所以,收藏Veh_info ==车?
如果在汽车类,真的,为什么位置type属性是不是数组?你在你的类定义有错误。
so, collection Veh_info==Car? if is that true, why Location type property in Car class is not array? You have errors in your class definition.
public class Car
{
public ObjectId Id{get;set;}
public string Color{get;set;}
public int Cly{get;set;}
public List<Location>Locs{get;set;}
}
比你可以用这个code:
than you can use this code:
MongoDB.Driver.MongoClient client = new MongoClient(connectionString);
var server = client.GetServer();
var db = server.GetDatabase("MyDbName");
var collection=db.GetCollection<Car>("Veh_info");
Car carById= collection.Find(Query<Car>.EQ(x=>x.Id,1));
if(carById!=null)
carById.Locs.Add(new Location(){loc="my new location name"});
collection.Save(carById);
这篇关于如何使用C#.NET插入MongoDB中阵列中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文