2

I have been searching through the world wide web for hours and I can't find a viable solution to my problem:

It is simple: I want to search through a collection of items from my MongoDB and return all the documents.

From a RESTFul API perspective : GET /items - Returns all items in the collection. Not sorted

// NOT WORKING - STILL TRYING  
    public static String getItems() {

        StringBuilder items = new StringBuilder();
    MongoCursor<Document> cursor = itemCollection.find().iterator();
    try {

        while (cursor.hasNext()) {
            items.append(cursor.next().toJson());

        }

    } finally {
        cursor.close();
    }

    return items.toString();
    }

As you can see I return a StringBuilder, since I want to concatenate every document into one big bulk. But this returns as TEXT and not JSON. See below:

This is when the read type is set as JSON
When JSON is set as read type

The below is when the read type is set to TEXT, which creates the output I need, but it is not the correct format.

When TEXT is set as read type

I can't return it as a Document and then use the method: toJson(), since it will only return the last entry. I've tried with Lists, but it can't be converted into JSON documents. The above is the closest I get to what I need.

I hope someone in here has been through the same issue as me and can give a quick tip to solve the issue I'm having :-).

3 Answers 3

2

You could collect the JSON strings in a List, and format that list as a JSON array string:

public static String getItems() {
    List<String> items = new ArrayList<>();
    MongoCursor<Document> cursor = itemCollection.find().iterator();
    try {
        while (cursor.hasNext()) {
            items.add(cursor.next().toJson());
        }
    } finally {
        cursor.close();
    }

    return "[" + String.join(", ", items) + "]";
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have to modify your code to properly form a collection of JSON objects

    while (cursor.hasNext()) {
        items.append(cursor.next().toJson());

    }

Is building for you the next output:

{json object1}{json object2}...{json objectN}

While you need

[{json object1},{json object2}, ... {json objectN}]

While concatenating JSONs you have missed [, , and ]

1 Comment

I've been blind staring the forgettable comma, and the surrounding with array brackets. Well, I will just post my solution below this! But thank you for the notice. Sometimes another set of eyes is helpful ;)
0

Based on the answer from Aleh Maksimovich, I can tell you that I fixed the issue by correcting it a comma between each document and wrap it all in in array of documents.

Here is the code for the solution:

/**
 * This method is used to retrieve all the items in the item collection
 * @return - A String which contains all the documents. 
 */
public static String getItems() {

    StringBuilder items = new StringBuilder();
    MongoCursor<Document> cursor = itemCollection.find().iterator();
    try {

        items.append("[");
        while (cursor.hasNext()) {

            items.append(cursor.next().toJson());
            if(cursor.hasNext()) {
                items.append(",");
            }
        }
        items.append("]");
    } finally {
        cursor.close();
    }

    return items.toString();
}

Thank you for your contribution and have a nice evening.

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.