Beispiel #1
0
        //1 找到状态为"initial"的事务文档
        private TransactionDocumentP RetrieveTransaction()
        {
            FilterDefinitionBuilder <TransactionDocumentP> filterBuilder = Builders <TransactionDocumentP> .Filter;
            FilterDefinition <TransactionDocumentP>        filter        = filterBuilder.Eq(doc => doc.State, "initial");

            return(mongoDBService.Single(TransactionCollectionName, filter));
        }
        /// <summary>
        /// 查找某一个子文档
        ///</summary>
        public void QueryChild()
        {
            FilterDefinitionBuilder <Model2> filterBuilder = Builders <Model2> .Filter;
            //找到父文档,过滤条件为Field2A=2并且Field2B=“2018-01-21T16:00:00.000Z”
            FilterDefinition <Model2> filterFather = filterBuilder.Eq(doc => doc.Field2A, "2")
                                                     & filterBuilder.Eq(doc => doc.Field2B, DateTime.Parse("2018-01-21T16:00:00.000Z"));


            //投影定义创建器:ProjectionDefinitionBuilder
            //用ProjectionDefinition过滤子文档,投影器创建器作用于Field2C,他是一个集合,用来保存多个子文档;过滤条件为Field1C = 3.1415926
            ProjectionDefinitionBuilder <Model2> projBuilder = Builders <Model2> .Projection;
            ProjectionDefinition <Model2>        proj        = projBuilder.ElemMatch(listField => listField.Field2C, childfield => childfield.Field1C == 3.1415926);

            FindOptions options = new FindOptions()
            {
                AllowPartialResults = true
            };
            Model2 info = mongoDBService.Single <Model2>(collectionName, filterFather, options, proj);
        }