简体   繁体   English

如何使用Spring Data MongoDB查询无效引用的文档

[英]How to query for documents with invalid references using spring data mongodb

Let's assume that we have collection A which references collection B through a field called "bref", which is an array of ObjectId's from collection B. For collection A, there are 4 possible situations that I am interested in: 假设我们有一个集合A,该集合通过一个称为“ bref”的字段引用了集合B,该字段是集合B中ObjectId的数组。对于集合A,我感兴趣的有4种情况:

  1. At least one bref array item points to a valid document from B. 至少一个bref数组项指向B中的有效文档。
  2. The bref array is empty. bref数组为空。
  3. The bref array does not exist (is therefore null). bref数组不存在(因此为null)。
  4. None of the items from bref array points to a valid B document. bref数组中的所有项目均未指向有效的B文档。

In spring-data, using a MongoRepository, I was able to resolve the situations from above as follows: 在Spring数据中,使用MongoRepository,我能够从上面解决以下情况:

  1. getByBrefLike(ObjectId brefId, Pageable pageable) getByBrefLike(ObjectId brefId,Pageable pageable)
  2. getByBrefIsNullOrBrefIsLessThanEqual(String[] emptyString, Pageable pageable) getByBrefIsNullOrBrefIsLessThanEqual(String [] emptyString,Pageable pageable)
  3. getByBrefIsNullOrBrefIsLessThanEqual(String[] emptyString, Pageable pageable) getByBrefIsNullOrBrefIsLessThanEqual(String [] emptyString,Pageable pageable)
  4. Not solved yet. 尚未解决。

Questions: 问题:

  1. Is it possible to detect the documents from A that points to non-existent references from B, using spring-data mongodb repository like I did in the above example? 是否可以像上例中那样使用spring-data mongodb存储库来检测A中指向B中不存在引用的文档?
  2. How would you prevent that situation from ever happening? 您将如何防止这种情况的发生? (is it possible to define some constraints)? (是否可以定义一些约束)?
  3. If a spring-data solution is not possible, what would a mongodb solution look like? 如果无法使用spring-data解决方案,mongodb解决方案将是什么样?

尝试这个

Pageable<T> findByBrefNotIn(List<ObjectId> brefs, Pageable, pageable)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM