Morphia实现类似select|Morphia实现类似select *,A from table的效果(A是虚拟字段)

【Morphia实现类似select|Morphia实现类似select *,A from table的效果(A是虚拟字段)】经过我的一番研究:

public void test() { /*OOO类就是Model类*/ Field[] fields = OOO.class.getDeclaredFields(); List projections = new ArrayList<>(); for(int i = 0; i < fields.length; i ++) { if(Modifier.isStatic(fields[i].getModifiers())) {/*去除静态变量*/ continue; } projections.add(Projection.projection(fields[i].getName())); } /*虚拟字段A的值来源于X字段与O字段之和*/ projections.add(Projection.projection("A", Projection.add("$X", "$O"))); AggregationPipeline pip = miDao.createAggregation() .project(projections.toArray(new Projection[projections.size()])); "$peerNominateCount"))) .sort(Sort.descending("A")); Iterator list = pip.aggregate(XXX.class); while(list.hasNext()) { System.out.println(list.next().toString()); } } /*XXX是专门为查询出的新数据设计的数据结构,当然你也可以把OOO类中的字段添加过来,或者直接继承它*/ private static class XXX{ private static class TestModel{@Id private ObjectId id; private Long A; public ObjectId getId() { return id; }public void setId(ObjectId id) { this.id = id; }public Long getA() { return A; }public void setA(Long A) { this.A = A; }@Override public String toString() { return "TestModel [id=" + id + ", A=" + A + "]"; } }}

    推荐阅读