Can you tell me when array of documents should be used and when array of objects should be used ?
Asked
Active
Viewed 2,313 times
1
-
These links will light up http://openmymind.net/Multiple-Collections-Versus-Embedded-Documents/ and http://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference – Digital Alchemist Nov 03 '16 at 18:45
1 Answers
0
By array of objects I'm assuming you mean ObjectId's AKA references to other collections, since a document is just a JSON object anyway.
The basic paradigm of data modeling is to embed whenever possible. If your collection references a finite number such a user's list of phone numbers like this - you definitely want to embed.
{
phone_numbers: [
{
type: "mobile",
number: "(123)456-7890"
},
{
type: "home",
number: "(456)789-0123"
}
]
}
If you are referencing a 1 <--> Many or 1 <--> Very Many collection, that is when you want to use references such as messages sent/received to a user.
{
from: ObjectId, // Reference to ObjectId of the sender
to: [], // Array of ObjectId references
message: String,
date: Date
}
I highly advise reading here:

dyouberg
- 2,206
- 1
- 11
- 21