2016-11-04 3 views
6

मैं जावा में निम्नलिखित मोंगो क्वेरी प्राप्त करना चाहता हूं।जावा ड्राइवरों का उपयोग कर मोंगोड में कॉलम को दबाने के लिए कैसे?

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

मैं जोड़ने के लिए { "_ id": 0} की जरूरत कोड तर्क में करने के लिए "_id" फ़ील्ड दबाएं। कृपया मुझे बताएं कि मैं इसे कैसे प्राप्त कर सकता हूं।

उत्तर

2

आप इस तरह कुछ कोशिश कर सकते हैं।

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

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

को बाहर करें अन्य क्षेत्रों

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

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

अनुमानों की पूरी सूची के लिए।

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

यह ठीक काम करता है ... महान जानकारी के लिए धन्यवाद साझा! –

संबंधित मुद्दे