/// <summary> /// 测试更新 /// </summary> public static void UpdateTest() { var ID = "601383d7d25a3a01e9714f93"; ObjectId id; if (!ObjectId.TryParse(ID, out id)) { throw new ArgumentOutOfRangeException("转换类型错误"); } Expression <Func <Role, bool> > filterWhere = x => x.Id == id; var update = new UpdateDefinition <Role>[] { Builders <Role> .Update.Set("UserName", "测试") }; var res = MongoDbHelper.Update(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Role, filterWhere, Builders <Role> .Update.Combine(update)); if (res) { Console.WriteLine("更新成功"); } else { Console.WriteLine("更新失败"); } }
/// <summary> /// 根据查询条件获取一条数据 /// </summary> public static void GetOneByConditionTest() { Expression <Func <Role, bool> > filterWhere = x => x.UserName.Contains("我的名字"); var result = MongoDbHelper.GetOneByCondition <Role>(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Role, filterWhere); if (result != null) { Console.WriteLine("姓名{0},年龄{1},状态{2}", result.UserName, result.Age, GetStateDesc(result.State)); } }
/// <summary> /// 查询测试 /// </summary> public static void QueryTest() { var list = MongoDbHelper.GetAll <Role>(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Role); if (list != null && list.Count > 0) { foreach (var item in list) { Console.WriteLine("姓名{0},年龄{1},状态{2}", item.UserName, item.Age, GetStateDesc(item.State)); } } }
/// <summary> /// 根据查询条件获取多条数据 /// </summary> public static void GetManyByConditionTest() { Expression <Func <Role, bool> > filterWhere = x => x.UserName.Contains("我的名字"); var list = MongoDbHelper.GetManyByCondition <Role>(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Role, filterWhere); if (list != null && list.Count > 0) { foreach (var item in list) { Console.WriteLine("姓名{0},年龄{1},状态{2}", item.UserName, item.Age, GetStateDesc(item.State)); } } }
/// <summary> /// 插入数据测试 /// </summary> public static void InsertTest() { var random = new Random(); for (int i = 0; i < 10; i++) { var item = new Role() { UserName = "******" + i, Age = random.Next(25, 30), State = i % 2 == 0?State.Normal:State.Unused }; MongoDbHelper.Insert(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Role, item); } }
/// <summary> /// 根据ID获取数据 /// </summary> public static void GetByIdTest() { var ID = "601383d7d25a3a01e9714f93"; ObjectId id; if (!ObjectId.TryParse(ID, out id)) { throw new ArgumentOutOfRangeException("转换类型错误"); } var result = MongoDbHelper.GetById <Role>(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Role, id); if (result != null) { Console.WriteLine("姓名{0},年龄{1},状态{2}", result.UserName, result.Age, GetStateDesc(result.State)); } }
/// <summary> /// 分页查询数据 /// </summary> public static void FindListByPageTest() { //var filter = Builders<Role>.Filter.Eq(c => c.State, State.Unused); var filter = Builders <Role> .Filter.Empty; var sortDocument = new BsonDocument("sort", 1); var sortDefinition = (SortDefinition <Role>)sortDocument; long Counts = 0; var list = MongoDbHelper.FindListByPage <Role>(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Role, filter, 1, 10, out Counts, sortDefinition); if (list != null && list.Count > 0) { foreach (var item in list) { Console.WriteLine("姓名{0},年龄{1},状态{2}", item.UserName, item.Age, GetStateDesc(item.State)); } } }
/// <summary> /// 删除数据 /// </summary> public static void DeleteTest() { var ID = "601383d7d25a3a01e9714f93"; ObjectId id; if (!ObjectId.TryParse(ID, out id)) { throw new ArgumentOutOfRangeException("转换类型错误"); } Expression <Func <Role, bool> > filterWhere = x => x.Id == id; var res = MongoDbHelper.DeleteByCondition(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Role, filterWhere); if (res) { Console.WriteLine("删除成功"); } else { Console.WriteLine("删除失败"); } }
static void Main(string[] args) { MongoDbHelper mongoDbHelper = new MongoDbHelper("Customer"); // sample document model var doc = new List <PersonModel> { new PersonModel { FirstName = "Tom", LastName = "Herry", }, new PersonModel { FirstName = "Elon", LastName = "Musk", }, new PersonModel { FirstName = "Jemmy", LastName = "wells", PrimaryAddress = new AddressModel { Street = "112 St", City = "Loss ", Zip = "Az8w7" } } }; // To insert the bulk record mongoDbHelper.InsertDocument <PersonModel>("Users", doc); // To read the record from the document var data = mongoDbHelper.ReadDocument <PersonModel>("Users"); foreach (var item in data) { Console.WriteLine($"Id: {item.Id}, First Name : {item.FirstName}, Last Name : {item.LastName}"); if (item.PrimaryAddress != null) { Console.WriteLine($"Adddress: {item.PrimaryAddress.Street},{item.PrimaryAddress.City}, {item.PrimaryAddress.Zip}"); } Console.WriteLine(); } var updateRecord = data.First(x => x.FirstName == "Tom"); updateRecord.PrimaryAddress = new AddressModel { Street = "London", City = "St", Zip = "12345" }; // To update the existing record mongoDbHelper.UpsertDocument("Users", updateRecord, updateRecord.Id); // To read the document by given id var filterdData = mongoDbHelper.ReadDocumentById <PersonModel>("Users", updateRecord.Id); Console.WriteLine($"Id: {filterdData.Id}, First Name : {filterdData.FirstName}, Last Name : {filterdData.LastName}"); if (filterdData.PrimaryAddress != null) { Console.WriteLine($"Adddress: {filterdData.PrimaryAddress.Street},{filterdData.PrimaryAddress.City}, {filterdData.PrimaryAddress.Zip}"); } // To delete the document by first name mongoDbHelper.DeleteDocument <PersonModel>("Users", filterdData.Id); Console.ReadLine(); }