Search code examples
javamongodbmorphia

Multiple search in mongodb with morphia


I've two classes: User and Project. The project class have ONE user (the owner of project).

In search method, it returns a list of user after a method, and with this list of users, i need to find all projects that contains the user of one item of the list os users that i have.

For solve this, i put a for and i call the find (morphia basicDao find method) for each user in for iteration, and the result i added in a array, and manually i remove the duplicated projects.

Someone knows a better method to solve my problemn? Maybe a morphia method that do this for me...

Sorry for bad english. :)


Solution

  • You can use the $in operator, which is documented online. As you might expect, Morphia's equivalent is called in, too.

    Here's an example of using in:

    List<String> userList;
    List<Project> projects = Project.find().field("user").in(userList).asList();