The problem

Recently, WHEN I used MongoDB in the project, I encountered a problem about how to query and return the specified fields. Baidu and Google Chinese searched all kinds of answers, but none of them was reliable. Let’s talk about the most widely circulated blog on Baidu that copied each other

       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);
Copy the code

This answer is clearly out of date, and BasicQuery can no longer be constructed this way.

To solve

Finally, I found the exact answer on StackOverflow.

		Query query = new Query();
		query.fields().include("path"); // Contains this field
		query.fields().exclude("salary");// This field is not included
Copy the code

reference

Stackoverflow.com/questions/3… Stackoverflow.com/questions/1… Docs. Spring. IO/spring – the data…