2016-11-04 9 views
6

Java'da aşağıdaki mongo sorgusuna ulaşmak istiyorum. Kod mantığında için:Java sürücüleri kullanarak mongodb'deki bir sütunu nasıl bastırırsınız?

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}) 

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); 

Ben {0 "_ID"} eklemem gerekiyor, ben şu java kodu var, ama bastırmak mantığı nasıl ekleneceğini emin değilim "_id" alanını bastır. Lütfen bunu nasıl başarabileceğimi bildirin.

cevap

2

Böyle bir şeyi deneyebilirsiniz.

import static com.mongodb.client.model.Projections.excludeId; 

FindIterable<Document> resultSet = db.getCollection("document").find(query).projection(excludeId()); 

projeksiyonların tam listesi için Diğer Alanlar

import static com.mongodb.client.model.Projections.fields; 

FindIterable<Document> resultSet = db.getCollection("document").find(query).projection(
fields(exclude("fieldname", "fieldvalue"))); 

hariç tutar. Büyük bilgi için

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

+0

sorunsuz çalışıyor ... Teşekkürler paylaştı! –