I am developing web application using Gin and MongoDB. I have defined models for user like,
type User struct {
Id nftdb.ObjID `bson:"_id,omitempty"`
Tenant string
Name string //common
Email string //common
Phone string //common
PAN string
ProfilePic string
Modules []string
Role string //CA,Individual,Business,Admin,etc.
AuthType string
AuthData map[string]any
Address Address
Registered bool //Registration Tracking
Verified bool // CA onBoarding Tracking
Status string // Active,Inactive
Extras map[string]any //keep all extra variable info in this!
ModifiedBy string
ModifiedDate time.Time
}
And Application model like this,
type Application struct {
Id nftdb.ObjID `bson:"_id,omitempty"`
Tenant string
Type string //CA, USER
User string //Refers to Userid
UserType string
SubmittedDate time.Time //Application submitted date
FinancialYear string //ITR Application
ModuleInfo []string //ITR Plans, Loan Selection
PaymentInfo *PaymentSummary
Reviewer string //Admin,CA (if ITR)
Comments string //Application Comments
Status string
ModifiedBy string
ModifiedDate time.Time
}
Every user has an application linked to him.I have to provide an endpoint which shows data like
"Username,EmailId,PhoneNumber,ApplicationSubmittedDate,Modules,ApplicationStatus". And also
I can filter the data based on Application Submitted Date and search by username.
Now How do I structure the query such that,I can get the intended data. Do I have to store the username field in Application so that querying based on username will be easier.But this will lead to data duplication.How do we approach these cases where we have to fetch data from 2 or more different collections and also need to provide filters based on different fields in different collections?
I have tried querying the applications collection first and after that filtered the Users collection from the ids which we get earlier from Application response. And but I am unable to get desired response when applying both filters like filtering applications within date range and search by username .For search by username,I am using regex search on the username field with case insensitive options.
From some blogs ,I have found out that ,we can used lookup (aggregation) to fetch results.But can we do it without aggregation ?