//准备转账 private TransactionDocument PrepareTransfer(double amt, string source, string destination) { string strGUID = System.Guid.NewGuid().ToString("N"); //类似e0a953c3ee6040eaa9fae2b667060e09 //创建事务文档 TransactionDocument tDoc = new TransactionDocument { _id = strGUID, //这个应该是随机生成的一个串 State = "new", Ts = DateTime.Now, Amt = amt, Src = source, Dst = destination }; //将事务文档插入事务集合 bool isSu = mongoDBService.Insert(TransactionCollectionName, tDoc); if (!isSu) { throw new Exception("构建事务文档失败!"); } FilterDefinitionBuilder <Account> filterBuilder = Builders <Account> .Filter; //更新source账户 FilterDefinition <Account> filterS = filterBuilder.Eq(m => m._id, source) & filterBuilder.Gte(m => m.Balance, amt); UpdateDefinition <Account> updateS = Builders <Account> .Update.Push(m => m.Txns, tDoc._id).Inc(m => m.Balance, -amt); UpdateResult updateResult = mongoDBService.DocumentUpdate(AccountsCollectionName, filterS, updateS); //检测更新是否成功 bool isSuccess = updateResult.ModifiedCount > 0 && updateResult.ModifiedCount == updateResult.MatchedCount? true:false; if (!isSuccess) { mongoDBService.Delete <TransactionDocument>(TransactionCollectionName, m => m._id == tDoc._id); throw new Exception("更新source账户失败"); } //更新destination账户 FilterDefinition <Account> filterD = filterBuilder.Eq(m => m._id, destination); var updateD = Builders <Account> .Update.Push(m => m.Txns, tDoc._id).Inc(m => m.Balance, amt); UpdateResult updateResultD = mongoDBService.DocumentUpdate(AccountsCollectionName, filterD, updateD); bool isSuccessD = updateResultD.ModifiedCount > 0 && updateResultD.ModifiedCount == updateResultD.MatchedCount ? true : false; if (!isSuccessD) { throw new Exception("更新destination账户失败"); } return(tDoc); }
/// <summary> /// 插入文档 /// </summary> public void Insert() { //Model2 model = new Model2 //{ // Id = Guid.NewGuid().ToString("N"), // Field2A = "2", // Field2B = DateTime.Now.Date, // Field2C = new List<Model1>() //}; //for (int i = 0; i < 3; i++) //{ // Model1 model1 = new Model1 // { // Field1A = "Welcome to the MongoDB", // Field1B = i, // Field1C = 3.1415926, // Field1D = 3.1415926F, // Field1E = new List<string> { "asd","dsa","sad"} // }; // model.Field2C.Add(model1); //} Model1 model1 = new Model1 { Field1A = "Welcome to the MongoDB", Field1B = 0, Field1C = 3.1415926, Field1D = 3.1415926F, Field1E = new List <string> { "asd", "dsa", "sad" } }; //插入一个collection bool t = mongoDBService.Insert(collectionName, model1); }
private static void CreateAccount() { AccountP accA = new AccountP { _id = "A001", Balance = 2010, PendingTransactions = new List <string>() }; AccountP accB = new AccountP { _id = "B001", Balance = 2010, PendingTransactions = new List <string>() }; mongoDBService.Insert(AccountsCollectionName, accA); mongoDBService.Insert(AccountsCollectionName, accB); }
//创建事务文档 private void PrepareTransfer(decimal value, string source, string destination) { //创建事务文档 TransactionDocumentP tDoc = new TransactionDocumentP { _id = string.Format("{0}For{1}", source, destination), State = "initial", LastModified = DateTime.Now, Value = value, Source = source, Destination = destination }; FilterDefinitionBuilder <TransactionDocumentP> filterBuilder = Builders <TransactionDocumentP> .Filter; FilterDefinition <TransactionDocumentP> filter1 = filterBuilder.Eq(doc => doc._id, tDoc._id); if (mongoDBService.ExistDocument(TransactionCollectionName, filter1)) { return; } //将事务文档插入事务集合 mongoDBService.Insert(TransactionCollectionName, tDoc); }