如何在 mongo db 中对引用集合使用条件查询 [英] How to use criteria query on refrence collection in mongo db

查看:54
本文介绍了如何在 mongo db 中对引用集合使用条件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在地址集合中找到所有拥有 city ="XYZ" 的人

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
}

推荐答案

Mongo 理解 @DBRef 作为对另一个文档的引用,在本例中为 Address 文档,最终当对象从 MongoDB 加载时,这些引用将被急切地解析,这将作为 HATEOAS 友好链接填充给用户.您将返回一个映射对象,该对象看起来与嵌入在您的主文档中存储的一样.

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.

您可以为给定的对象定义您的存储库,它将端点映射到您的数据库,例如下面定义的 PersonRepository 作为示例:

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. 第一个查询是获取具有 city = "XYZ" 的地址文档.从返回的列表中解析 id.
  2. 使用上一个操作中的 ID 对 Person 实体生成另一个查询.
  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.

下面演示了这种方法

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

如果您事先知道地址 ID,则可以使用自定义查询:

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

这篇关于如何在 mongo db 中对引用集合使用条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆