1.指定查询哪些字段
方式一:
Query query = new Query(); query.fields().include("booking"); //包含该字段 query.fields().exclude("salary");//不包含该字段
Booking booking = mongoTemplate.findOne(query, Booking.class);
@Test public void testName11() throws Exception { Query query = Query.query(Criteria.where("bookingNo").is("UFL-BK2020070711530323920")); Booking booking = new Booking(); query.fields().include("bookingNo"); query.fields().include("milestoneStatus"); query.fields().include("bookingDateLoc"); List<Booking> bookingList = mongoTemplate.find(query, Booking.class); System.err.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.err.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.err.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.err.println(JSON.toJSONString(bookingList));
方式2:
QueryBuilder queryBuilder = new QueryBuilder(); queryBuilder.or(new BasicDBObject("onumber", "002"), new BasicDBObject("cname","zcy1")); BasicDBObject fieldsObject=new BasicDBObject(); fieldsObject.put("onumber", 1); fieldsObject.put("cname", 1); Query query=new BasicQuery(queryBuilder.get(),fieldsObject)