C# Asp.net中简单操作MongoDB数据库(一) , mongodb数据库连接可以回顾上面的篇幅。
1、model类:
public class BaseEntity { /// <summary> /// 字段映射,告诉mongodb这个字段在数据库中对应_id /// </summary> [BsonId] //告诉mongodb这个字段在数据库中的类型是ObjectId [BsonRepresentation(BsonType.ObjectId)] public string _id { get; set; } } public class IPInfo : BaseEntity { public string ip { get; set; } public string ipAddress { get; set; } public string addTime { get; set; } } public class IPDetails : BaseEntity { /// <summary> /// 主表id /// </summary> [BsonRepresentation(BsonType.ObjectId)] public string parentId { get; set; } public string ipAddress { get; set; } public string ipNetwork { get; set; } public string ipBrowser { get; set; } public string addTime { get; set; } } 自定义实体类
2、基本操作:
public class IPInfoFactory { /// <summary> /// 集合(表)名称 /// </summary> private static readonly string CollectionName = "IPInfo"; //ip子表名称 private static readonly string ChildCollectionName = "IPDetail"; private static IMongoDatabase db = MongoDb.GetMongoDb(); public static List<IPInfo> GetAllList() { try { var collection = db.GetCollection<IPInfo>(CollectionName); List<IPInfo> list = collection.Find(new BsonDocument()).ToList(); return list; } catch (Exception ex) { return null; } } /// <summary> /// 插入主表 /// </summary> /// <returns></returns> public static bool Add(string ip, string ipAddress) { try { var IPInfoCollection = db.GetCollection<IPInfo>(CollectionName); IPInfo ipInfo = new IPInfo(); ipInfo.ip = ip; ipInfo.ipAddress = ipAddress; ipInfo.addTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); IPInfoCollection.InsertOne(ipInfo); return true; } catch (Exception ex) { return false; } } /// <summary> /// 插入主表、子表: /// 主表:有则不插入,无则插入; /// 子表:一条数据都没有,则插入,已经存在数据,判断是否存在一条相同数据,存在则不插入,不存在则插入。 /// </summary> /// <param name="ip">ip</param> /// <param name="model">ip接口获取的信息</param> /// <param name="ipBrowser">浏览器</param> /// <returns></returns> public static bool AddIPInfoAndDetail(string ip, ApiIPInfo model, string ipBrowser = "") { try { //ip地址 var ipAddress = model.Address; //ip网络 var ipNetwork = model.Network; var IPInfoCollection = db.GetCollection<IPInfo>(CollectionName); var IPDetailCollection = db.GetCollection<IPDetails>(ChildCollectionName); //判断主表是否已经存在 var pFilter = Builders<IPInfo>.Filter.Eq("ip", ip); var parentItem = IPInfoCollection.Find(pFilter).FirstOrDefault(); if (parentItem == null) { //插入主表 IPInfo ipInfo = new IPInfo(); ipInfo.ip = ip; ipInfo.ipAddress = ipAddress; ipInfo.addTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); IPInfoCollection.InsertOne(ipInfo); //插入子表 string parent_id = string.Empty; //刚插入的主表 var parentItemNew = IPInfoCollection.Find(pFilter).FirstOrDefault(); if (parentItemNew != null) { parent_id = parentItemNew._id; AddChild(parent_id, ipAddress, ipNetwork, ipBrowser); } } else { string parent_id = parentItem._id; var childFilter1 = Builders<IPDetails>.Filter.Eq("parentId", parent_id); var childItem = IPDetailCollection.Find(childFilter1).FirstOrDefault(); if (childItem == null) { AddChild(parent_id, ipAddress, ipNetwork, ipBrowser); } else { //如果没有类似的数据,则插入 var filterBuilder = Builders<IPDetails>.Filter; var childFilter2 = filterBuilder.Eq("parentId", parent_id) & filterBuilder.Regex("ipNetwork", new BsonRegularExpression(new Regex(ipNetwork))) & filterBuilder.Regex("ipBrowser", new BsonRegularExpression(new Regex(ipBrowser))); var childItem2 = IPDetailCollection.Find(childFilter2).FirstOrDefault(); if (childItem2 == null) { AddChild(parent_id, ipAddress, ipNetwork, ipBrowser); } } } return true; } catch (Exception ex) { return false; } } /// <summary> /// 插入子表 /// </summary> /// <returns></returns> public static bool AddChild(string parentId, string ipAddress, string ipNetwork, string ipBrowser) { try { var IPDetailCollection = db.GetCollection<IPDetails>(ChildCollectionName); IPDetails ipDetails = new IPDetails(); ipDetails.parentId = parentId; ipDetails.addTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); ipDetails.ipAddress = ipAddress; ipDetails.ipNetwork = ipNetwork; ipDetails.ipBrowser = ipBrowser; IPDetailCollection.InsertOne(ipDetails); return true; } catch (Exception ex) { return false; } } /// <summary> /// 插入子表 /// </summary> /// <returns></returns> public static bool AddChild(IPDetails model) { try { var IPDetailCollection = db.GetCollection<IPDetails>(ChildCollectionName); IPDetails ipDetails = new IPDetails(); ipDetails.parentId = model.parentId; ipDetails.addTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); ipDetails.ipAddress = model.ipAddress; ipDetails.ipNetwork = model.ipNetwork; ipDetails.ipBrowser = model.ipBrowser; IPDetailCollection.InsertOne(ipDetails); return true; } catch (Exception ex) { return false; } } }