我有以下两个文档结构。在结构CRMContact.orgGroupId == OrganizationGroup.id中。我想获取所有与sharedGroupIds匹配的CRMContact文档,还从CRMContact中仅选择几个字段,从OrganizationGroup中仅选择OrganizationGroup.groupownername并匹配/填充 groupId (仅包含一个字段[ groupownername ])。我已经在下面的自定义实现中使用过,但是没有用。
我包括了aggregarionsNotWorking,它不能正常工作,并且aggregarions返回了整个OrganizationGroup。如何实现此目标,即仅使用Spring数据mongodb填充groupownername字段?
@Document(collection = "ww_crm_contact")
public class CRMContact{
@Id
protected String id;
private String displayName;
private String firstName;
private String middleName;
private String lastName;
private OrganizationGroup groupId; //Ignore //Modified field name orgGroupId
@Indexed(name = "CRMCONTACT_SHAREDGROUPID_IDX",background = true)
private List<String> sharedGroupIds = new LinkedList<>();
@Indexed(name = "CRMCONTACT_ORGGROUPID_IDX",background = true)
private String orgGroupId;
}
@Document(collection = "ww_organization_groups")
public class OrganizationGroup {
private static final long serialVersionUID = 600049975643062552L;
@Id
protected String id;
private String groupName;
private int riaId;
private Boolean isPrivate;
private String description;
private Boolean deleted;
@Transient
private int count;
private String groupownerid;
private String groupownername;
}
@Repository
public class CustomCRMContactDAO {
@Autowired
MongoTemplate mongoTemplate;
public List<CRMContact> getContactsPresentInGroup(List<ObjectId> objectIds){
LookupOperation lookupOperation = LookupOperation.newLookup().from("ww_organization_groups").localField("orgGroupId").foreignField("_id").as("groupId");
ProjectionOperation fields = project("firstName","lastName", "primaryId","displayName","groupId.groupownername");
Aggregation aggregarionsNotWorking = Aggregation.newAggregation(Aggregation.match(Criteria.where("sharedGroupIds").in(objectIds)),lookupOperation,unwind("groupId"),fields); //Not Working even if I change the field only to groupownername
Aggregation aggregarions = Aggregation.newAggregation(Aggregation.match(Criteria.where("sharedGroupIds").in(objectIds)),lookupOperation,fields); //
List<CRMContact> crmContacts = mongoTemplate.aggregate(aggregarions, "ww_crm_contact",CRMContact.class).getMappedResults();
return crmContacts;
}
}