我正在尝试从spring框架的mongoTemplete的executeCommand执行“db.post.find()。pretty()”之类的查询。但我无法做到这一点?有没有办法直接从mongotempelate执行上面的查询?任何帮助表示赞赏。
这是我的代码:
public CommandResult getMongoReportResult(){
CommandResult result=mongoTemplate.executeCommand("{$and:[{\"by\":\"tutorials point\"},{\"title\": \"MongoDB Overview\"}]}");
return result;
}
答案 0 :(得分:1)
是的当然,但你应该传递git parameter plugin作为参数,而不是String,如下所示: (并且您的命令格式不正确,请参阅BasicDBObject
BasicDBList andList = new BasicDBList();
andList.add(new BasicDBObject("by", "tutorials point"));
andList.add(new BasicDBObject("title", "MongoDB Overview"));
BasicDBObject and = new BasicDBObject("$and", andList);
BasicDBObject command = new BasicDBObject("find", "collectionName");
command.append("filter", and);
mongoTemplate.executeCommand(command);