Java MongoClient - 使用Aggregate时键入异常

时间:2016-06-16 09:46:50

标签: java mongodb mongodb-query mongodb-java

我收到以下exception

com.mongodb.CommandFailureException: { "serverUsed" : "192.168.3.25:27017" , 
         "errmsg" : "exception: The argument to $size must be an Array,
          but was of type: EOO" , "code" : 17124 , "ok" : 0.0}

每当我使用MongoClient的{​​{1}}驱动程序运行此管道时:

Java

造成这种情况的原因是什么?管道在[{ "$match" : { "_id" : "1"}}, { "$project" : { "count" : { "$size" : "$tags"}}}] shell?

中工作正常

这是我的Mongo代码:

Java

数据库结构:

  private void countArrayLength(String id, String arrayName) {

      AggregationOptions options = AggregationOptions.builder().build(); 

      DBObject matchFields = new BasicDBObject("_id", id);
      DBObject match = new BasicDBObject("$match", matchFields);

      DBObject projectCount = new BasicDBObject("$size", "$" + arrayName);
      DBObject projectFields = new BasicDBObject("count", projectCount);
      DBObject project = new BasicDBObject("$project", projectFields);

      Cursor cursor = db.getCollection(collectionName)
                        .aggregate(asList(match, project), options);
}

0 个答案:

没有答案