0

I am Using SpringData Mongo for storing and retrieving data to/from mongoDB.

In my web application I have a admin(super user), who can view its sub user list(Direct Agents), admin is represented by his account, for each super user/admin there are multiple sub user(DAs).

I have below JSON to represent my data:

{
    "_id" : ObjectId("5a60bf4d52deb4984c5d87c8"),
    "_class" : "com.abc.xyz.DirectAgentsMongoRecord",
    "account" : NumberLong(1234),
    "directAgentDetailsMongoRecords" : [ 
        {
            "daAccount" : NumberLong(987654),
            "name" : "Anil",
            "location" : "vfdgfds",
            "mobileNo" : "09876543",
            "commission" : "8",
            "isActive" : true
        }, 
        {
            "daAccount" : NumberLong(5432),
            "name" : "Sudhir",
            "location" : "frewrwe",
            "mobileNo" : "233223",
            "commission" : "11",
            "isActive" : false
        }, 
        {
            "daAccount" : NumberLong(5432),
            "name" : "Bikas",
            "location" : "frewrwe",
            "mobileNo" : "233223",
            "commission" : "10",
            "isActive" : false
        }, 
        {
            "daAccount" : NumberLong(5432),
            "name" : "Pankaj",
            "location" : "frewrwe",
            "mobileNo" : "1234",
            "commission" : "10",
            "isActive" : false
        }
    ]
}

Records in Spring Data Mongo:

@Document(collection)
public class DirectAgentDetailsMongoRecord{

    private Long daAccount;

    private String name;

    private String organisation;

    private String location;

    private String mobileNo;

    private BigDecimal dues;

    private BigDecimal creditLimit;

    private BigDecimal commission;

    private Boolean isActive;
//getter setter
}

@Document(collection = "BODirectAgents")
public class DirectAgentsMongoRecord{

    @Id
    private String id;

    private Long account;

    private List<DirectAgentDetailsMongoRecord> directAgentDetailsMongoRecords;

//getter and setters
}

I have to write find method with search criteria account and any field from subuser list and should return list of sub user(directAgentDetailsMongoRecords).

I have written repository with below method:

@Query(value = "{ 'account' : ?0, 'directAgentDetailsMongoRecords.name' : ?1 }")
    DirectAgentsMongoRecord findByAccountAndDirectAgentDetailsMongoRecordsName(Long account, String agentName);

But above method will return me the superuser with all sub user record in it.

I have two questions:

  1. On the sub user array list, I have to perform filter, sort operations, auto suggest etc, further one admin will never require data of another. Currently, I am using single collection for all admins/super user. Is it good to have separate collections for all admins? Currently we have 2000+ admins and that list might grow.

  2. In Single Collection, how do I perform operations(sort, search, get single column data) on child element and return only list of child element? Example method:

    DirectAgentDetailsMongoRecord[] findByAccountAndDirectAgentDetailsMongoRecordsName(Long account, String agentName);

Bikas Katwal
  • 1,895
  • 1
  • 21
  • 42
  • 1
    Possible duplicate of [Create filter aggregation in spring](https://stackoverflow.com/questions/46767750/create-filter-aggregation-in-spring) – s7vr Jan 19 '18 at 10:17
  • You will require $filter aggregation through mongo template. For issues regarding embedding vs reference you should prefer embedding and later split the documents into multiple documents when size grows. – s7vr Jan 19 '18 at 10:18
  • if you have many records in admin's sublist, then it's better to move it outside of admins collrction and filter then by additional attributes. If each admin have a little entries in the list, it's better to get fill record of concrete admin and filter it's sublist on client side – Alexey Sviridov Apr 24 '18 at 07:05

0 Answers0