Hello @amyjian @Erik_Hatcher :
This deserves some discussion. I came from the relational database world (Postgres/Mysql) Data is accessed via joining tables, which are separated using the forms of normalization. Coming to MongoDb, I watched the Data Modelling videos that MongoDB officially provides. These make a VERY STRONG case for not using relationships between collections/tables, rather embedding related tables as sub-documents.
The use case of a database is to store and access data. If following this modelling video’s advice I set up my data as sub-documents, and cannot search and get the id of a matching sub-document, then we have a serious problem. Either Mongo should not recommend sub-documents as the preferred data modelling design, or it should provide the ability to search sub documents.
In any case, now am not sure what to do. Here is my index and this index belongs to the Users Collection. Therefore the ‘deos’ is a sub-document in the Users collection, and it contains a field called ‘text’ which I am searching:
{
"mappings": {
"dynamic": false,
"fields": {
"deos": {
"fields": {
"text": {
"type": "string"
}
},
"type": "document"
}
}
}
}
`
The use case is as follows. I have a Users Collection. Each user has multiple Leos and Deos. I have set up sub-documents for Leos and Deos as follows:
leos Array (3)
deos Array (4)
firstname
Arjun
lastname
Kochhar
.
.
.
Basically I need to match a text field in the leos sub-document to a text field in the deos sub-document. The match using Atlas Search works and I am able to obtain the matching text. However I cannot get the id of the deo document that matches a leo document or vice versa. This is basic and quite key. I need to know which document in leos matches which one in deos. There is no workaround and hack. It should be straightforward if you are recommending not to use separate tables but sub-documents to get the id of the items that match. Using filter in an aggregation pipeline is a hack, and quite a dirty way to do it. But if i were to use that approach how do I do it? Isnt there a more straightforward way?
To Reiterate:
Now I have a USERS collection and this contains sub collections of related user data. I use the Atlas Search to do a text search on field in a nested or sub -document in the USER collection and using PROJECT I am able to get the fields for the USER where there was a match in the sub-document. However I cannot for the life of me get the _id of the sub document where the actual match took place!
So then do I have to take out my Sub-Documents and create separate collections? This goes against the whole principle of Mongo Db data modelling that is detailed in your videos.
If I do take them out, and make separate collections for Leos and Deos, how do I relate these to my Users. Is this the right way to go from a data modelling perspective.
Currently I am building the system so I have a few Users, each with a few Leos and Deos. But when the system is live we hope to have millions of Users each with hundreds of Leos and Deos.
How do I approach this? Am stuck. Can you also point me to some videos /resources/sessions on data modelling in Mongo Db. I am not sure if using sub-documents is the way to go, when I need to search and match sub-documents with each other. This is a very basic requirement. If I was not using sub-documents, the way it is done in relational databases is that searches are done across tables. Here searches are done across sub-documents, only I cannot get the _id, then that defeats the whole purpose.
Google says the following about relating data in MongoDb:
In MongoDB, relationships between data can be managed using embedded documents and references : Embedded Documents: This approach stores related data within a single document, ideal for data that is frequently accessed together. It simplifies data
retrieval and ensures data locality.
- Are embedded documents different from simply creating sub-documents as an array of objects nested in the parent document?
- How does one do Atlas Search on fields in embedded/sub documents and return the id where there is a match rather than just the matching text?
This is then a question about how to model data in mongo db and how to do searches for sub-documents. Please address how data should be modelled in Mongo db given that search is almost always required.
Please suggest how to proceed.
Thanks.