Skip to content
Advertisement

How do I retrieve particular List of element from MongoDB Using findById from another List => SubList from MainList?

How To Retrieve this list:

[
    {
        "id": "1",
        "title": "Jordanian Mansaf",
        "description": "content: blah blah blah, method: blah blah blah",
        "imagePath": "Mansaf imagePath",
        "created": "2022-03-12T04:42:39.818+00:00"
    },
    {
        "id": "2",
        "title": "Jordanian Maklobah",
        "description": "content: blah blah blah, method: blah blah blah",
        "imagePath": "Maklobah imagePath",
        "created": "2022-03-12T04:43:10.869+00:00"
    },
    {
        "id": "3",
        "title": "Suadi Kabsah",
        "description": "content: blah blah blah, method: blah blah blah",
        "imagePath": "Kabsah imagePath",
        "created": "2022-03-12T04:43:36.470+00:00"
    },
    {
        "id": "4",
        "title": "Briani",
        "description": "content: blah blah blah, method: blah blah blah",
        "imagePath": "Briani imagePath",
        "created": "2022-03-12T04:44:03.227+00:00"
    },
    {
        "id": "5",
        "title": "Zarb",
        "description": "Contnent: blah blah blah, Method: blah blah blah",
        "imagePath": "Zarb_imagePath",
        "created": "2022-03-12T11:43:26.330+00:00"
    }
]

When passing this list [id]:

[
    {
        "id": "1",
        "title": "Cat1",
        "imagePath": "Cat1_imagePath",
        "created": "2022-03-12T04:42:39.818+00:00"
    },
    {
        "id": "2",
        "title": "Cat2",
        "imagePath": "Cat2_imagePath",
        "created": "2022-03-12T04:43:10.869+00:00"
    },
    {
        "id": "3",
        "title": "Cat3",
        "imagePath": "Cat3_imagePath",
        "created": "2022-03-12T04:43:36.470+00:00"
    },
    {
        "id": "4",
        "title": "Cat4",
        "imagePath": "Cat4 imagePath",
        "created": "2022-03-12T04:44:03.227+00:00"
    },
    {
        "id": "5",
        "title": "Food Cat5",
        "imagePath": "Cat5_imagePath",
        "created": "2022-03-12T11:43:26.330+00:00"
    }
]

Advertisement

Answer

I just figured out an answer for my question and it’s as follows:

In the mongo repository just declare a custom find method:

 @Repository
public interface RecipeRepository extends MongoRepository<RecipeContainer, String> {
   List<RecipeContainer> findAllById(String id);
}

Then use it as a normal find method => [query] in your business logic layer and controller layer.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement