代码之家  ›  专栏  ›  技术社区  ›  Debopam

springdatamongodb匹配、查找和投影,只从已查找的文档中选择所需字段

  •  0
  • Debopam  · 技术社区  · 6 年前

    我有以下两个文档结构。在结构中CRMContact.orgGroupId== 组织组.id。我想获取与sharedGroupIds匹配的所有CRMContact文档,并且只从CRMContact和OrganizationGroup.groupownername从OrganizationGroup和match/populate (只有一个字段[ 组所有者名称 ]已填充)。我使用了下面的自定义实现,但没有工作。

    我包括了aggregarionsNotWorking,它不工作,aggregarions返回整个组织组填充。如何实现这一点,即使用springdatamongodb填充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;
    
        }
    
    }
    
    0 回复  |  直到 6 年前