1

I wanted to add all of the ID's inside of MongoDB to an array with using Java. How do I take ID's or any data form and put in an array from JAVA?

PS: I'm newbie with nosql database structure and I'm using MongoDb 3.2

Code is so far something like this.

public void ArrayEx(){

    MongoClient mongoClient = new MongoClient("localhost",27017);
    MongoDatabase database = mongoClient.getDatabase("dbTest");
    MongoCollection<Document> collection =database.getCollection("colTest");
    MongoCursor<Document> cursor = collection.find().iterator();
    while(cursor.hasNext()) {

      //What should I add here?

    }
}
3
  • You want to have a long[] of _id ? Commented Mar 9, 2017 at 13:08
  • I want to reach every _id put them in array so that I can create a combobox of it. Commented Mar 9, 2017 at 13:10
  • You can try MongoCursor<Document> cursor = collection.find().projection(new Document("_id", 1)).iterator(); while(cursor.hasNext()) { Document result = cursor.next(); Long id = result.getLong("_id"); // add to combobox } Commented Mar 9, 2017 at 13:32

2 Answers 2

1

If you want only the ids, you can use the mongodb distinct function

List Ids = collection.distinct("_id");
Sign up to request clarification or add additional context in comments.

2 Comments

I wanted to put them in array one by one so that I can create a combobox automatically.
I got the Idea from your answer, but I have to write some more code ;-) List<ObjectId> ids = StreamSupport.stream(collection.distinct("_id", ObjectId.class).spliterator(), false).collect(Collectors.toList());
0

Modify your method as follows :

public void ArrayEx() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("dbTest");
    MongoCollection<Document> collection = database.getCollection("colTest");
    MongoCursor<Document> cursor = collection.find().iterator();

    ArrayList<String> listOfIDS = new ArrayList<>();
    while (cursor.hasNext()) {
        listOfIDS.add(cursor.next().getObjectId("_id").toString());
    }

    String ids[] = new String[listOfIDS.size()];
    ids = listOfIDS.toArray(ids);
}

7 Comments

Here's the thing, I'm using String, when I try to do with your example it says String cannot be converted to Long, after that when I change your codes with String this time it says Object cannot be converted to String, so what should I do?
On which line it says String cannot be converted to Long ?
It says on the localhost, I'm not sure in which line but probably problem occurs in here " listOfIDS.add(cursor.next().getLong("_id")); " because when I change here to String that case it says ObjectId cannot be cast to String.
Are you using MongoDB's default _id or you have overridden the _id to be long? I mean are you sure your _id field is long?
My _id field should be String, then again I cannot use String too because It says ObjectId cannot be cast to String...
|

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.