Example #1
0
        //3 执行转账
        private bool ApplyTransaction(TransactionDocumentP t, decimal value, string source, string destination)
        {
            FilterDefinitionBuilder <AccountP> filterBuilderS = Builders <AccountP> .Filter;
            FilterDefinition <AccountP>        filterS1       = filterBuilderS.Eq(doc => doc._id, source);
            var updateS = Builders <AccountP> .Update.Inc(m => m.Balance, -value).Push(m => m.PendingTransactions, t._id);

            UpdateResult updateResultS = mongoDBService.DocumentUpdate(AccountsCollectionName, filterS1, updateS);

            bool isSuss = updateResultS.ModifiedCount > 0 && updateResultS.ModifiedCount == updateResultS.MatchedCount;

            if (isSuss)
            {
                FilterDefinitionBuilder <AccountP> filterBuilderD = Builders <AccountP> .Filter;
                FilterDefinition <AccountP>        filterD1       = filterBuilderD.Eq(doc => doc._id, destination);
                var updateD = Builders <AccountP> .Update.Inc(m => m.Balance, value).Push(m => m.PendingTransactions, t._id);

                UpdateResult updateResultD = mongoDBService.DocumentUpdate(AccountsCollectionName, filterD1, updateD);
                isSuss = updateResultD.ModifiedCount > 0 && updateResultD.ModifiedCount == updateResultD.MatchedCount;
            }

            return(isSuss);
        }
        /// <summary>
        /// 插入子文档
        /// </summary>
        public void InsertChild()
        {
            Model1 model1 = new Model1
            {
                Field1A = "MongoDB是一种开源文档型数据库",
                Field1B = 100,
                Field1C = 3.1415926,
                Field1D = 3.1415926F,
                Field1E = new List <string>()
            };

            Model2 model2 = new Model2
            {
                Id      = new Guid().ToString("N"),
                Field2A = "1",
                Field2B = DateTime.Now.Date,
                Field2C = new List <Model1>()
            };


            FilterDefinitionBuilder <Model2> filterBuilder = Builders <Model2> .Filter;
            //过滤条件字段Field2A==2
            FilterDefinition <Model2>      filter      = filterBuilder.Eq(doc => doc.Field2A, "2");
            SortDefinitionBuilder <Model1> sortBuilder = Builders <Model1> .Sort;
            //按字段Field1A升序排列
            SortDefinition <Model1> sort = sortBuilder.Ascending(pu => pu.Field1A);
            //最新插入的在最前面
            UpdateDefinitionBuilder <Model2> updateBuilder = Builders <Model2> .Update;
            //PushEach 参数含义:
            //field:lambda表达式,要更新的字段,这里其实是个集合,集合里的每一个元素都是一个子文档。
            //values:待放入field的值,这里是一个集合,所以一次可以放入多个
            //slice:约束子文档集合的大小,如果设置为null,则大小不限,如果设置为0,那么没有任何子文档可被装入其中。
            //position:指定插入子文档在集合中的位置
            //sort:对子文档进行排序,可以指定任意字段排序
            UpdateDefinition <Model2> update = updateBuilder.PushEach(doc => doc.Field2C, new List <Model1> {
                model1
            }, null, 0, sort);
            UpdateResult updateResult = mongoDBService.DocumentUpdate(collectionName, filter, update);
        }
        //准备转账
        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);
        }