以下是我用来排序的代码:
db.getCollection('employee').aggregate([
{
$project: {
fieldToSortWith: { $toLower: "$lastName" },
subject: 1
}
},
{
$sort: { fieldToSortWith: 1 },
}
])
这就是你在春季数据中的做法
DBCollection coll = mongoTemplate.getCollection("employee");
List<DBObject> pipe = new ArrayList<>();
DBObject prjflds = new BasicDBObject();
prjflds.put("id", 1);
prjflds.put("firstName", 2);
prjflds.put("lastName", new BasicDBObject("$toLower", "$lastName"));
DBObject project = new BasicDBObject();
project.put("$project", prjflds);
pipe.add(project);
DBObject sort = new BasicDBObject();
sort.put("$sort", new BasicDBObject("lastName", 1));
pipe.add(sort);
BasicDBObject skip = new BasicDBObject("$skip", searchCriteria.getCurrentPage() * searchCriteria.getPageSize());
BasicDBObject limit = new BasicDBObject("$limit", searchCriteria.getPageSize());
pipe.add(skip);
pipe.add(limit);
BasicDBObject employeeRoleFilter = new BasicDBObject("$match", new BasicDBObject("employeeRoles", new BasicDBObject("$in", searchCriteria.getEmployeeRolesNames())));
pipe.add(employeeRoleFilter);
BasicDBObject employeeStatusFilter = new BasicDBObject("$match", new BasicDBObject("active", new BasicDBObject("$eq", Boolean.valueOf(searchCriteria.getEmployeeStatus()))));
pipe.add(employeeStatusFilter);
searchCriteria.isEmployeeNameSearchIndicator());
pipe.add(searchTextFilter);
coll.aggregate(pipe).results();
之后,您将获得需要在您的域中转换的本机 BasicDBObjects。如果您需要任何帮助,请告知此代码是混乱的。