问题描述
我在mongodb中有一个集合,其模式如下所示
@Document(collection = "organizations")
@Getter
@Setter
@ToString
public class Organization
{
@Id
private String id;
private String name;
// id of parent org
private String parent;
}
现在,我想检查一个组织是否是其他组织的子组织,因此我正在尝试使用graphlookup。我的情况是,一个人创建了一条记录,只有其主管可以看到其记录。为此,我正在检查一个组织是否像另一个组织一样。
目前我正在尝试这种方式
public boolean checkOrganizationIsInSub(String parent,String child) {
GraphLookupOperation graphLookupOperation = GraphLookupOperation.builder().
from("organizations").startWith("$id").connectFrom("id")
.connectTo("parent").as("children");
final Criteria byParentId = new Criteria("id").is(parent);
//final Criteria bychildID = new Criteria("id").is(child);
final MatchOperation matchStage = Aggregation.match(byParentId);
Projectionoperation countProj = Aggregation.project().and("children").size().as("count");
Aggregation aggregation = Aggregation.newAggregation(matchStage,graphLookupOperation,countProj);
List<Integer> count = mongoTemplate.aggregate(aggregation,Organization.class,Integer.class).getMappedResults();
System.out.println(count);
if(count.size() == 0)
return false;
else
return true;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)