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:
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.
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);