public bool UpdateEndusers(EndUser[] newUsers, out string msg) { Mongo mongo = new Mongo(connectionString); MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase; MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>; try { mongo.Connect(); for (int i = 0; i < newUsers.Length; ++i) { List<EndUser> userList = new List<EndUser>(); Document enduser1 = new Document(); enduser1["AutoID"] = newUsers[i].AutoID; enduser1["EnduserName"] = newUsers[i].EnduserName; enduser1["SimCardNo"] = newUsers[i].SimCardNo; enduser1["Identities"] = getIdentitiesString(newUsers[i].Identities); enduser1["LastModifyTime"] = newUsers[i].LastModifyTime; enduser1["IsValid"] = newUsers[i].IsValid; Document doc = new Document { { "SimCardNo", newUsers[i].SimCardNo } }; if (doc == null) { msg = "不存在该SIM卡号:" + newUsers[i].SimCardNo.ToString(); mongo.Disconnect(); return false; } mongoCollection.Update(new Document("$set", enduser1), doc); } msg = "更新成功"; mongo.Disconnect(); return true; } catch (Exception ex) { msg = ex.Message; mongo.Disconnect(); return false; } }
public EndUser[] SelectEnduserByName(string iEnduserName, out string msg) { Mongo mongo = new Mongo(connectionString); MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase; MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>; try { mongo.Connect(); List<EndUser> userList = new List<EndUser>(); MongoRegex reg = new MongoRegex(".*" + iEnduserName + ".*", MongoRegexOption.IgnoreCase); Document docEnduserName = new Document { { "name", reg } }; IEnumerable<Document> doc = mongoCollection.Find(docEnduserName).Documents; foreach (Document d in doc) { EndUser user = new EndUser(); user.AutoID = d["AutoID"].ToString(); user.EnduserName = d["EnduserName"].ToString(); user.Identities = d["Identities"].ToString().Split('|'); user.IsValid = bool.Parse(d["IsValid"].ToString()); user.LastModifyTime = DateTime.Parse(d["LastModifyTime"].ToString()); user.SimCardNo = d["SimCardNo"].ToString(); userList.Add(user); } msg = "查询成功"; mongo.Disconnect(); return userList.ToArray(); } catch (Exception ex) { msg = ex.Message; mongo.Disconnect(); return null; } }
public bool UpdateEnduser(EndUser newUser, out string msg) { Mongo mongo = new Mongo(connectionString); MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase; MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>; try { mongo.Connect(); List<EndUser> userList = new List<EndUser>(); Document enduser1 = new Document(); enduser1["AutoID"] = newUser.AutoID; enduser1["EnduserName"] = newUser.EnduserName; enduser1["SimCardNo"] = newUser.SimCardNo; enduser1["Identities"] = getIdentitiesString(newUser.Identities); enduser1["LastModifyTime"] = newUser.LastModifyTime; enduser1["IsValid"] = newUser.IsValid; Document doc = new Document { { "SimCardNo", newUser.SimCardNo } }; if (doc == null) { msg = "不存在该SIM卡号"; mongo.Disconnect(); return false; } mongoCollection.Update(new Document("$set", enduser1), doc); //IEnumerable<Document> doc2 = mongoCollection.Find(new Document { { "SimCardNo", newUser.SimCardNo } }).Documents; //foreach (Document d in doc2) //{ // EndUser user = new EndUser(); // user.AutoID = d["AutoID"].ToString(); // user.EnduserName = d["EnduserName"].ToString(); // user.Identities = d["Identities"].ToString().Split('|'); // user.IsValid = bool.Parse(d["IsValid"].ToString()); // user.LastModifyTime = DateTime.Parse(d["LastModifyTime"].ToString()); // user.SimCardNo = d["SimCardNo"].ToString(); // userList.Add(user); //} msg = "更新成功"; mongo.Disconnect(); return true; } catch (Exception ex) { msg = ex.Message; mongo.Disconnect(); return false; } }
public bool InsertEndusers(EndUser[] newUsers, out string msg) { Mongo mongo = new Mongo(connectionString); MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase; MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>; try { mongo.Connect(); for (int i = 0; i < newUsers.Length; ++i) { IEnumerable<Document> doc = mongoCollection.Find(new Document { { "SimCardNo", newUsers[i].SimCardNo } }).Documents; if (doc == null || doc.Count() == 0) { Document enduser1 = new Document(); enduser1["AutoID"] = newUsers[i].AutoID; enduser1["EnduserName"] = newUsers[i].EnduserName; enduser1["SimCardNo"] = newUsers[i].SimCardNo; enduser1["Identities"] = getIdentitiesString(newUsers[i].Identities); enduser1["LastModifyTime"] = newUsers[i].LastModifyTime; enduser1["IsValid"] = newUsers[i].IsValid; mongoCollection.Save(enduser1); } else { msg = "用户已存在"; mongo.Disconnect(); return false; } } msg = "插入成功"; mongo.Disconnect(); return true; } catch (Exception ex) { msg = ex.Message; mongo.Disconnect(); return false; } }