How to use criteria query on refrence collection in mongo db

让人想犯罪 __ 提交于 2019-12-24 17:22:32

问题


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
}

回答1:


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);
}


来源:https://stackoverflow.com/questions/33385037/how-to-use-criteria-query-on-refrence-collection-in-mongo-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!