0

I am trying to collect data from my mongodb in java, I need to use select query and put it in jtextarea. Select Query will be filled with combo box element.

Here is the code:

/**** Connect to 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();

In tableCollection.find shows:

cannot convert from FindIterable to MongoCursor

Is there any way to do this differently?

2 Answers 2

1

You are trying to receive a FindIterable object in a MongoCursor reference.

Change thetype of the reference variable 'cursor' from MongoCursor to FindIterable or its supertype MongoIterable.

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

(or)

MongoIterable<Document> cursor = tableCollection.find(whereQuery);
Sign up to request clarification or add additional context in comments.

3 Comments

your code doesn't show anything, i tried to put it in jtextarea but cannot get result, by the way thank you for your effort :).
You have to iterate over the result to get your data.
How to do that? i am new at java and i do not like Java at all :D.
0

I have found code for this problem and it works, main part is in code below

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*/
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.