예제 #1
0
        /// <summary>
        /// 获得所有子文档
        /// 这种方法不能够获得指定条件的所有子文档,要想选出想要的子文档要用聚集操作
        /// </summary>
        public void QueryChildren()
        {
            //投影定义创建器:ProjectionDefinitionBuilder
            //用ProjectionDefinition过滤子文档
            ProjectionDefinitionBuilder <Model2> projBuilder = Builders <Model2> .Projection;
            ProjectionDefinition <Model2>        proj        = projBuilder.ElemMatch(listField => listField.Field2C, childfield => childfield.Field1B == 0);

            List <Model2> info = mongoDBService.List <Model2>(collectionName,
                                                              m => m.Field2A == "2" && m.Field2B == DateTime.Parse("2018-01-21T16:00:00.000Z"), proj);
        }
        //清理,这个方法应该定期执行
        //清理方法也是“幂等的”
        public void CleanupTransactions(TimeSpan maxTxnTime)
        {
            //原书中for txn in db.transaction.find({ 'state': 'commit' }, {'_id': 1}):中的1不对,去掉
            List <TransactionDocument> docCommitList = mongoDBService.List <TransactionDocument>(TransactionCollectionName, m => m.State == "commit");

            foreach (TransactionDocument tdoc in docCommitList)
            {
                RetireTransaction(tdoc);
            }

            DateTime now    = DateTime.Now.ToUniversalTime();
            DateTime cutOff = now - maxTxnTime;
            //找出超时操作 回滚
            List <TransactionDocument> docRoolbackList = mongoDBService.List <TransactionDocument>(TransactionCollectionName, m => m.Ts.CompareTo(cutOff) < 0 && m.State == "new");

            foreach (TransactionDocument tdoc in docRoolbackList)
            {
                RollbackTransfer(tdoc);
            }
        }