earl
earl

Reputation: 768

Ignore _id from Mongo Query spring framework library

I am to run following query for my micro service through Mongo template.

find({"accId": 1234}, {"_id":0}).

I am using Query and Criteria libraries. I have tried various combinations of the addCriteria but I am unable to reach the desired query.

Any inputs or guidance here ?

Upvotes: 0

Views: 844

Answers (1)

Valijon
Valijon

Reputation: 13093

Try this:

@Autowired
private MongoTemplate mongoTemplate;
...

Query q = new Query(Criteria.where("accId").is(1234));
q.fields().exclude("_id");
List<YourClass> result = mongoTemplate.find(q, YourClass.class);

https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/query/Query.html

Upvotes: 2

Related Questions