2

Hello so I am trying to get list of IDs from mongoDB , wrote this code that returns map of id:value I just want it to return just values .

        query=new Query(Criteria.where("_id").is("47b3b1ab-2d80-42cf-b289-e3d45497b59f"));
        query.fields().include("recordList.id").exclude("_id");
        System.out.println( mongoTemplate.findOne(query, Map.class,"Company3"));
{recordList=[{id=rec4vCGPy3EnXRuCM}, {id=recAivYlqtDzZP62C}, {id=recbcLfxuLLB6Jjn0}, {id=reckIA8RdQtDUKCYI}, {id=rectnZZzBJ2iKN8eO}]}

But I need something like this

[rec4vCGPy3EnXRuCM, recAivYlqtDzZP62C, recbcLfxuLLB6Jjn0, reckIA8RdQtDUKCYI, rectnZZzBJ2iKN8eO]

Yes I know I can manipulate result like this to get desired result but I want to know if its possible to achieve same result directly from DB and not like this

        List<Map<String,String>> list = (List<Map<String, String>>) mongoTemplate.findOne(query, Map.class,"Company3").get("recordList");

        List<String> idList=new ArrayList<>();
        for (Map<String, String> stringStringMap : list) {
            idList.add(stringStringMap.get("id"));
        }

This is what my data looks like mongodb document. Sorry for inserting image , couldnt copy it without it being unreadable .

3 Answers 3

1

oblivion02's solution was a little bit wrong but definitely hinted me in right direction , thank you.

Query query=new Query(Criteria.where("_id").is("adfe377d-6e5b-48f0-b5bb-12b09f57285d"));
System.out.println(mongoTemplate.findDistinct(query,"recordList.id","Company4",String.class));

Just these two lines give me a nice clean list of just id values

[rec4vCGPy3EnXRuCM, recAivYlqtDzZP62C, recbcLfxuLLB6Jjn0, reckIA8RdQtDUKCYI]
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe distinct could help. https://docs.mongodb.com/manual/reference/method/db.collection.distinct/

Query query=new Query(Criteria.where("_id").is("adfe377d-6e5b-48f0-b5bb-12b09f57285d"));

System.out.println(mongoTemplate.findDistinct(query,"recordList.id","Company4",String.class));

2 Comments

@NikolozLatsabidze - Will update the answer! Happy I was able to help in some way.
Your answer definitely helped out a lot , couldnt find single solution out there . Thanks.
0

You can not do that using Mongodb. This database is document oriented, meaning that given a criteria (in this case, an id), you will get a list of documents satifying the criteria where each document has some properties and some values.

To make it easier, you could rewrite your code so you could map your result to a pojo which only contains the list of ids you want and no key.

It would be something similar to the following:

public class Result {

private List<String> ids;

// getters and setters here

@override
public String toString(){
    return ids.toString();
}

}

Now your repository method retrieving data will look like the following:

query = new Query(Criteria.where("_id").is("47b3b1ab-2d80-42cf-b289-e3d45497b59f"));

// No need for this //query.fields().include("recordList.id").exclude("_id");

System.out.println( mongoTemplate.findOne(query, Result.class,"Company3"));

2 Comments

Well the solution of this post answers your post , thank you anyways !
Great, something new I've learned :). No problem!

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.