我正在执行以下mongodb查询,我是mongo db的新手,请告诉我我做错了什么
db.entityCounter.aggregate([
{
$lookup:
{
from: "fields",
localField: "code",
foreignField: "fieldCode",
as: "fieldsresult"
}
},{
$match:{
$and: [{
"fieldsresult.isVisible":"1"
},{"type":"field"
}]
}
}])
下面是Java春季代码
LookupOperation lookupOperation = LookupOperation.newLookup()
.from("fields")
.localField("code")
.foreignField("fieldCode")
.as("fieldsresult");
AggregationOperation match1 = Aggregation.match(Criteria.where("fieldsresult.isVisible").is("1"));
// AggregationOptions aggregationOptions = Aggregation.newAggregationOptions();
DBObject ob=new BasicDBObject();
((BasicDBObject) ob).put("batchSize",10);
Aggregation aggregation = Aggregation.newAggregation(lookupOperation,match1).withOptions(Aggregation.newAggregationOptions().cursor(ob).build());
long val=0;
try {
AggregationResults<EntityCounter> result = mongoOperations.aggregate(aggregation, Fields.class, EntityCounter.class);
// val= result.getMappedResults();
}catch (Exception e){
e.printStackTrace();
}
但是我遇到了错误
org.springframework.dao.InvalidDataAccessApiUsageException:命令执行失败:错误[需要'cursor'选项,但带有explain参数的聚合除外,Command = {“ aggregate”:“ entityCounter”,“ pipeline”:[ {“ $ match”:{“ fieldsresult.isVisible”:“ 1”}},{“ $ lookup”:{“ from”:“ fields”,“ localField”:“ code”,“ foreignField”:“ fieldCode”, “ as”:“ fieldsresult”}}]};嵌套异常为com.mongodb.MongoCommandException:命令失败,错误为9:服务器本地主机:27017上,“光标”选项是必需的,但带有说明参数的聚合除外。完整的响应为{“ ok”:0.0,“ errmsg”:“'cursor'选项是必需的,除了带有explain参数的聚合”,“ code”:9,“ codeName”:“ FailedToParse”}
答案 0 :(得分:-1)
在mongodb 3.4中引入了查找功能,请升级您的dB