1

我正在尝试从spring框架的mongoTemplete的executeCommand执行诸如“db.post.find().pretty()”之类的查询。但是我做不到?有没有办法直接从mongotempelate执行上面的查询?任何帮助表示赞赏。

这是我的代码:

public CommandResult getMongoReportResult(){
    CommandResult result=mongoTemplate.executeCommand("{$and:[{\"by\":\"tutorials point\"},{\"title\": \"MongoDB Overview\"}]}");
    return result;
}
4

1 回答 1

2

是的,当然,但你应该传递一个BasicDBObject作为参数,而不是一个字符串,像这样:(并且你的命令格式不正确,请参阅find 命令

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);
于 2017-03-27T10:31:14.170 回答