1

How do I find all the person which are having city ="XYZ" in Address collection

public class Person { 
 @Id
private String id;

private String description

@DBRef
private Address address;

// Getters and Setters
}

public class Address
{
@Id
private String id;

private String area

private String city

// Getters and Setters
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Anil Darji
  • 45
  • 2
  • 8

1 Answers1

0

Mongo understands @DBRef as a reference to another document, in this case, an Address document and ultimately when the object is loaded from MongoDB, those references will be eagerly resolved and this will get populated to the user as a HATEOAS friendly link. You will get back a mapped object that looks the same as if it had been stored embedded within your master document.

You can define your repository, which will map the endpoints to your database, for the given object, like PersonRepository defined below as an example:

import com.mycompany.domain.Person;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PersonRepository extends MongoRepository<Person, String> {

    List<Person> findByCity(@Param("city") String city);

}

Another way you could go around this using the query criteria methods is executing two queries.

  1. First query would be to fetch the address documents which have the city = "XYZ". Resolve the ids from the list returned.
  2. Generate another query on the Person entity using the ids from the previous operation.

The following demonstrates this approach

Query addressQuery = new Query(where("city").is("XYZ"));
addressQuery.fields().include("_id");
List<Address> addressList = mongoTemplate.find(addressQuery, Address.class, "address"); // get the addresses list that satisfy the given city criteria

// Resolve the ids for the addresses
final List<ObjectId> addressIds = new ArrayList<ObjectId>(addressList.length);      
for(final Address address : addressList) {
    addressIds.add(new ObjectId(address.getId()));
}

// Get the Person list using the ids from the previous operation
Query personQuery = new Query(where("address.$id").in(addressIds));
List<Person> list = mongoTemplate.find(personQuery, Person.class, "person");

If you knew the address id before hand you can then use a custom query:

public interface PersonRepository extends MongoRepository<Person, String> {

    @Query("{ 'address': {'$ref': 'address', '$id': { '$oid': ?0 } } }")
    List<Person> findByAddres(String addressIdAsString);
}
chridam
  • 100,957
  • 23
  • 236
  • 235