显示数据时MongoDB出错

时间:2018-01-11 05:09:51

标签: java spring mongodb spring-mvc

  

2018-01-11 10:34:57 ERROR ConsumerLoadDAOImpl:357 -   org.springframework.dao.InvalidDataAccessApiUsageException:命令   执行失败:错误['光标'选项是必需的,除了   与explain参数聚合],

Command =

{  
   "aggregate":"Product",
   "pipeline":[  
      {  
         "$group":{  
            "_id":"$productCode",
            "id":{  
               "$first":"$_id"
            },
            "productName":{  
               "$first":"$productName"
            },
            "description":{  
               "$first":"$description"
            },
            "productCode":{  
               "$first":"$productCode"
            },
            "eanCode":{  
               "$first":"$eanCode"
            },
            "categoryId":{  
               "$first":"$categoryId"
            },
            "subcategoryId":{  
               "$first":"$subcategoryId"
            },
            "productLineId":{  
               "$first":"$productLineId"
            },
            "brandId":{  
               "$first":"$brandId"
            },
            "mrp":{  
               "$first":"$mrp"
            },
            "discountedPrice":{  
               "$first":"$discountedPrice"
            },
            "consumerId":{  
               "$first":"$consumerId"
            },
            "status":{  
               "$first":"$status"
            },
            "createdDateTime":{  
               "$first":"$createdDateTime"
            },
            "attachmentId":{  
               "$first":"$attachmentId"
            }
         }
      }
   ]
}; 

nested exception is com.mongodb.CommandFailureException:

{  
   "serverUsed":"localhost:27017",
   "ok":0.0,
   "errmsg":"The 'cursor' option is required, except for aggregate with the explain argument",
   "code":9,
   "codeName":"FailedToParse"
}

任何人都可以帮助我在调用控制台显示数据时在控制台中收到这些错误。

jdk = 1.8
mongodb = 3.6
spring = 4.x

这是控制器代码

public List<Product> searchProductWithAggregationWithPagination(Class<?> voName,String column,Integer currentPage,Integer noOfRecord) {

    List<Product> list1 = new ArrayList<Product>();
    try {
        GroupOperation groupByStateAndSumPop = group("productCode").
                first("id").as("id").
                first("productName").as("productName").
                first("description").as("description").
                first("productCode").as("productCode").
                first("eanCode").as("eanCode").
                first("categoryId").as("categoryId").
                first("subcategoryId").as("subcategoryId").
                first("productLineId").as("productLineId").
                first("brandId").as("brandId").
                first("mrp").as("mrp").
                first("discountedPrice").as("discountedPrice").
                first("consumerId").as("consumerId").
                first("status").as("status").
                first("createdDateTime").as("createdDateTime").
                first("attachmentId").as("attachmentId");

        MatchOperation matchStage = Aggregation.match(new Criteria("status").is(true));

        Aggregation agg = null;
        if(currentPage == 0 && noOfRecord == 0 && column.isEmpty()) {
            agg = newAggregation(groupByStateAndSumPop);
        }else {
            SortOperation sortByAvgPopAsc = sort(new Sort(Direction.DESC,column));
            agg = newAggregation(groupByStateAndSumPop
                    ,skip(currentPage),
                    limit(noOfRecord),matchStage,sortByAvgPopAsc);
        }

        AggregationResults<Product> result = mongoTemplate.aggregate(agg, Product.class, Product.class);

        list1 = result.getMappedResults();

    } catch (Exception e) {
        LOGGER.error(e);
    }
    return list1;
}

0 个答案:

没有答案