How to suppress a column in mongodb using Java drivers?

I would like to get the following mongo request in Java.

db.getCollection('document').find({ "$and": [ {"storeId":"1234"}, {"tranDate" : {"$gte":new Date("Sat,01 Oct 2016 00:00:00 GMT"), "$lte":new Date("Mon,31 Oct 2016 00:00:00 GMT")}} ] }, {"_id" : 0}) 

I have the following java code, but I'm not sure how to add suppression logic,

 List<Bson> conditions = new ArrayList<>(); conditions.add(eq("field1", "value1")); conditions.add(eq("field2", "value2")); Bson query = and(conditions); FindIterable<Document> resultSet = db.getCollection("document").find(query); 

I need to add {"_id": 0} in the code logic to suppress the "_id" field. Please let me know how I can achieve this.

+6
source share
1 answer

You can try something like this.

 import static com.mongodb.client.model.Projections.excludeId; FindIterable<Document> resultSet = db.getCollection("document").find(query).projection(excludeId()); 

Exclude other fields

 import static com.mongodb.client.model.Projections.fields; FindIterable<Document> resultSet = db.getCollection("document").find(query).projection( fields(exclude("fieldname", "fieldvalue"))); 

Full list of forecasts.

http://api.mongodb.com/java/3.0/?com/mongodb/client/model/Projections.html http://mongodb.imtqy.com/mongo-java-driver/3.0/builders/projections/

+2
source

Source: https://habr.com/ru/post/1012022/


All Articles