我收到以下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);
}