/// <summary>
        /// 删除一个子文档
        /// </summary>
        public void DeleteSubDoc()
        {
            //过滤器作用与Field2C字段,过滤条件是Field1B = 1
            var update = Builders <Model2> .Update.PullFilter(m => m.Field2C, (y => y.Field1B == 2));

            //父文档过滤条件为Field2A=2,如果匹配出多个父文档,只操作第一个文档
            mongoDBService.UpdateOne <Model2>(collectionName, m => m.Field2A == "2", update);
        }
示例#2
0
        //5 更新两个账户的待处理事务链表,移除事务标识
        private bool UpdateAccount(TransactionDocumentP t, string source, string destination, TimeSpan maxTxnTime)
        {
            FilterDefinitionBuilder <AccountP> filterBuilderS = Builders <AccountP> .Filter;
            FilterDefinition <AccountP>        filterS        = filterBuilderS.Eq(doc => doc._id, source);
            var updateS = Builders <AccountP> .Update.Pull(doc => doc.PendingTransactions, t._id);

            bool isSucc = mongoDBService.UpdateOne(AccountsCollectionName, filterS, updateS);

            while (true)
            {
                if (isSucc)
                {
                    break;
                }
                bool timeOut = CheckTimeOut(t, maxTxnTime);
                if (timeOut)
                {
                    break;
                }
                isSucc = mongoDBService.UpdateOne(AccountsCollectionName, filterS, updateS);
            }
            if (!isSucc)
            {
                return(isSucc);
            }

            FilterDefinitionBuilder <AccountP> filterBuilderD = Builders <AccountP> .Filter;
            FilterDefinition <AccountP>        filterD        = filterBuilderD.Eq(doc => doc._id, destination);
            var updateD = Builders <AccountP> .Update.Pull(doc => doc.PendingTransactions, t._id);

            isSucc = mongoDBService.UpdateOne(AccountsCollectionName, filterD, updateD);
            while (true)
            {
                if (isSucc)
                {
                    break;
                }
                bool timeOut = CheckTimeOut(t, maxTxnTime);
                if (timeOut)
                {
                    break;
                }
                isSucc = mongoDBService.UpdateOne(AccountsCollectionName, filterD, updateD);
            }
            return(isSucc);
        }