2016-11-04 1 views
6

Javaで次のようなmongoクエリーを達成したいと思います。コードのロジック内に:Javaドライバを使用してmongodbの列を抑制する方法は?

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

は私が{0「_id」}を追加する必要があり、私は、次のJavaコードを持っているが、私は抑制ロジックを追加するかどうかはわかりません"_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

それが正常に動作します...おかげで共有します! –

関連する問題