2016-10-21 5 views
0

ようにするために使用モンゴ-javaのドライバ3.0.4と私たちのコード - のmongo-javaのドライバ3.3.0にこれを変換する問合せMongoのコレクション

DBCollection dbCollection = mongoClient.getDB(databaseName).getCollection(collectionName); 
QueryBuilder queryBuilder = new QueryBuilder(); 
/** queryBuilder.put() for building the query */ 
DBCursor dbCursor = dbCollection.find(queryBuilder.get()); 
while(dbCursor.hasNext()) { 
    DBObject dbObject = dbCursor.next(); 
    // add entries to a list of TDocument type 
} 

、Iこれで終わった -

MongoCollection<TDocument> collection = database.getCollection(collectionName, TDocument.class); //where TDocument is custom document class of ours 
QueryBuilder queryBuilder = new QueryBuilder(); 
/** queryBuilder.put() for building the query */ 
FindIterable<TDocument> tDocumentList = collection.find(queryBuilder.get()); //this is not compiling 
for (TDocument element : tDocumentList) { 
    names.add(element.getName()); //addition to some list of TDocument type 
} 

しかし、ポイントは、私はまだ私が定義したMongoDBのコレクションのfind操作のソースをコンパイルすることはできませんしています。

ここで何を修正する必要がありますか? mongoを3.3.0+にアップグレードするのに役立つ任意の実装に固執したいと思います。

編集 - マイ(違っlibの名前から下に命名)TDocumentクラスクラスのような単純なPOJOである -

public class TDocType { 

    private TDocType() { 
    } 

    String one; 

    @NotNull 
    String name; 

    String third; 

    String fourth; 

    // getter and setter for all the above 
} 
+0

からフィルタを使用する 変更?それはDBObjectを拡張しますか?そのクラスを投稿に追加します。 – Veeram

+0

@Veeramが追加されたが、なぜそれが必要なのか分からなかった。 – nullpointer

+1

あなたはそれをcast.find(Bson)queryBuilder.get()にキャストすることができます – Veeram

答えて

2

Bsonにキャストします。

FindIterable<TDocument> tDocumentList = collection.find((Bson)queryBuilder.get()); 

更新::あなたのTDocumentがどのように見えるんかモンゴ3.3.0

Bson filter = Filters.eq("field", "value"); 
FindIterable<TDocument> tDocumentList = collection.find(filter); 
+0

(stackoverflow.com/questions/40179940)同様に続けることができる一貫したアプローチに基づいてこれに解決策をマークしますか?それに続くことができる一貫したアプローチに基づいてこれに対する解決策を示すだろう – nullpointer

1

あなたはorg.bson.Document等により、クエリビルダを置き換えることができますこの:

そして

FindIterable<TDocument> tDocumentList = collection.find(query); //this is not compiling 
+0

コードの最後の行を修正する - > 'query.get()'は動作しません。 'Object'型の' key'が必要です。 – nullpointer

+0

私の悪い、それは単なるクエリーではありませんquery.get() – vincent

+0

これも見てください(stackoverflow.com/questions/40179940)?あなたがこれを見ることができます – nullpointer

関連する問題