简体   繁体   English

如何从Spring的mongo模板执行mongo查询?

[英]How to execute mongo query from spring's mongo template?

I am trying to execute queries like "db.post.find().pretty()" from executeCommand of mongoTemplete of spring framework. 我正在尝试从spring框架的mongoTemplete的executeCommand执行类似“ db.post.find()。pretty()”的查询。 But I am unable to do it?Is there a way to execute queries like above directly from mongotempelate? 但是我做不到,有没有办法直接从mongotempelate执行上述查询? Any help is appreciated. 任何帮助表示赞赏。

Here is my code: 这是我的代码:

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

yes of course, but you should pass a BasicDBObject as parameter, and not a String, like this: (and your command wasn't formatted correctly, see find command 是的,当然,但是您应该传递BasicDBObject作为参数,而不是像这样的String :(并且您的命令格式不正确,请参见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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM