Mongo 中的不同查询使用 Java Mao 访问模式
Distinct Query in Mongo using Java Mao Access Pattern
我想 运行 使用 spring 框架 java API 在 mongo 数据库中进行不同的查询。查询是这样的:
db.location.distinct("state");
如何为上述内容创建查询对象。下面是一个失败的尝试。
import org.springframework.data.mongodb.core.query.Query;
public List<Location> getLocations(int skipCount, int pageSize)
{
Query query = new Query(Criteria.where("state").is("distinct);
return mongoOperations.find(query, Location.class);
/*
Don't want to do the below, as using Mao Pattern --
DBCollection collection = db.getCollection("location");
DBObject o1 = new BasicDBObject();
List list = collection.distinct("state", o1);
return list;
*/
}
实际上没有 .distinct()
直接在 MongoOperations 上的方法,也没有任何真正转化为一个的方法,保存聚合框架。
但你可以直接从底层 Collection 对象中获取方法:
List states = mongoOperations.getCollection("location").distinct("state");
当然需要提供集合名称,因为来自 .getCollection()
的集合对象来自底层 Java 驱动程序。
或者如果你愿意,你可以直接运行 "command" 形式,但你仍然需要为集合命名:
BasicDBObject distinctCommand = new BasicDBObject("distinct", "location")
.append("key", "state");
List states = (List)mongoOperation.executeCommand(distinctCommand).get("values");
我想 运行 使用 spring 框架 java API 在 mongo 数据库中进行不同的查询。查询是这样的:
db.location.distinct("state");
如何为上述内容创建查询对象。下面是一个失败的尝试。
import org.springframework.data.mongodb.core.query.Query;
public List<Location> getLocations(int skipCount, int pageSize)
{
Query query = new Query(Criteria.where("state").is("distinct);
return mongoOperations.find(query, Location.class);
/*
Don't want to do the below, as using Mao Pattern --
DBCollection collection = db.getCollection("location");
DBObject o1 = new BasicDBObject();
List list = collection.distinct("state", o1);
return list;
*/
}
实际上没有 .distinct()
直接在 MongoOperations 上的方法,也没有任何真正转化为一个的方法,保存聚合框架。
但你可以直接从底层 Collection 对象中获取方法:
List states = mongoOperations.getCollection("location").distinct("state");
当然需要提供集合名称,因为来自 .getCollection()
的集合对象来自底层 Java 驱动程序。
或者如果你愿意,你可以直接运行 "command" 形式,但你仍然需要为集合命名:
BasicDBObject distinctCommand = new BasicDBObject("distinct", "location")
.append("key", "state");
List states = (List)mongoOperation.executeCommand(distinctCommand).get("values");