2017-11-10 4 views
0

私はJavaでmongodbからデータを収集しようとしていますが、selectクエリを使用してjtextareaに入れる必要があります。選択クエリはコンボボックスの要素で満たされます。ここJavaを使用してMongoDBバージョン3.4からデータを取得する方法は?

コードである:tableCollection.findで

/****のMongoDBに接続****/

MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017"); 
MongoClient mClient = new MongoClient(connectionString); 

/**** Get database ****/ 
MongoDatabase db = mClient.getDatabase("productDB"); 

mClient.getAddress(); 
/**** Get collection/table from 'productDB' ****/ 
MongoCollection<Document> tableCollection = db.getCollection("local"); 
/**** Find and display ****/ 
Document whereQuery = new Document(); 
whereQuery.put("Product Category",categoryCB.getSelectedIndex()); 



MongoCursor<Document> cursor = tableCollection.find(whereQuery); 
mClient.close(); 

示す:

がにFindIterableへ変換することができませんMongoCursor

これを行う方法はありますか?

答えて

0

FindIterable<Document> cursor = tableCollection.find(whereQuery); 

(または)は、主要部分は、あなたのコードは何も表示されません

mClient = new MongoClient(connectionString); 
db = mClient.getDatabase("Your Database Name"); 

tableCollection = db.getCollection("Your Table Name"); 
whereQuery = new Document(); 

whereQuery.put("Your Attribute", typeCB.getSelectedItem().toString()); 
iterator=tableCollection.find(whereQuery); 
cursor = iterator.iterator(); 

while (cursor.hasNext()) 
{ 
     /*put your code here*/ 
} 
1

FindIterableオブジェクトをMongoCursorの参照で受信しようとしています。 'カーソル' はMongoCursorからFindIterable又はそのスーパータイプMongoIterableを参照変数の

変更thetype。私は、この問題のためのコードを発見したと、それが動作

MongoIterable<Document> cursor = tableCollection.find(whereQuery); 
+0

以下のコードであり、私が試してみましたそれをjtextareaに入れても結果は得られません。あなたの努力に感謝します:)。 – Selim

+0

データを取得するには、結果を反復処理する必要があります。 – Tharun

+0

これを行うにはどうすればいいですか?私はJavaで新しく、Javaはまったく好きではない:D。 – Selim

関連する問題