'MongoDb - Store "reference" of Object in Array within the same document
To be blunt, I've not a lot of experience in MongoDB, I have trawled the internet, but cannot seem to find anything in relation to my question. And apologies if this seems completely backward.
I'm using Spring Data's MongoDB Starter with a Spring Boot application.
Instead of repeating data within my document, I'd rather "reference" (I know that has a different meaning in Mongo terms) so that I do not have to repeat data. I guess if this was a SQL database, we'd normalise this to have another table. Do I have to do this with NoSQL? Am I thinking about it in the incorrect way?
Considering I have the following Document, you can see I reference an object within the Addresses Array, is there any way to specifically do this within the same document?
User {
Addresses : [
{AddressLine1 : "Test Street"},
......
],
Emails: [
{email : "[email protected]", verified : true},
......
],
Defaults : {
deliveryAddress : "/Addresses/0",
recoveryEmail : "/Emails/0"
}
}
I have thought about having an identifier on the address, and perhaps having the defaults as something that is populated from a query (i.e where the address list has a "deliveryAddress" type - use that one)
User {
Addresses : [
{AddressLine1 : "Test Street", type : "delivery"},
......
],
Emails: [
{email : "[email protected]", verified : true},
......
],
Defaults : {
deliveryAddress : "/Addresses/0",
recoveryEmail : "/Emails/0"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
