/// <summary> /// 修改机器人实体 /// </summary> /// <param name="robotEntity"></param> /// <returns></returns> public static bool UpdateRobotMasterEntity(Model.RobotEntity robotEntity) { try { string robotMasterCollectionName = Common.ConfigFileHandler.GetAppConfig("RobotMasterCollectionName"); var collection = MongodbHandler.GetInstance().GetCollection(robotMasterCollectionName); var filterID = Builders <BsonDocument> .Filter.Eq("_id", ObjectId.Parse(robotEntity.id)); BsonDocument update = robotEntity.ToBsonDocument(); //不能更新ID update.Remove("_id"); //更新 BsonDocument updateResult = collection.FindOneAndUpdate(filterID, update); if (updateResult != null) { return(true); } return(false); } catch (Exception ex) { throw ex; } }
/// <summary> /// 插入机器人 /// </summary> /// <param name="robotEntity"></param> /// <returns></returns> public static bool InsertRobotMasterEntity(Model.RobotEntity robotEntity) { try { string robotMasterCollectionName = Common.ConfigFileHandler.GetAppConfig("RobotMasterCollectionName"); var collection = MongodbHandler.GetInstance().mc_MongoDatabase.GetCollection <Model.RobotEntity>(robotMasterCollectionName); collection.InsertOne(robotEntity); return(true); } catch (Exception ex) { throw ex; } }
/// <summary> /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点 /// </summary> /// <returns></returns> public static MongodbHandler GetInstance() { // 当第一个线程运行到这里时,此时会对locker对象 "加锁", // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁 // lock语句运行完之后(即线程运行完之后)会对该对象"解锁" // 双重锁定只需要一句判断就可以了 if (uniqueInstance == null) { lock (locker) { // 如果类的实例不存在则创建,否则直接返回 if (uniqueInstance == null) { uniqueInstance = new MongodbHandler(); } } } return(uniqueInstance); }