DIfference between sub docs and population in Mongoose and doubts about database schema DIfference between sub docs and population in Mongoose and doubts about database schema mongoose mongoose

DIfference between sub docs and population in Mongoose and doubts about database schema


In answer to part one, the difference is that in a Document-Subdocument scenario the subdocument is stored with the document (as a child document, accessible through the use of dot notation).

In the Mongoose populate scenario, a reference is held to a document in another collection. When you tell mongoose to populate what it does is make another query to MongoDB to get this 'subdocument'.

The main difference I see is that with the mongoose populate method you cannot query for documents using the 'subdocument' properties, whereas you can with the embedded subdocument model.


Edit, subdocument example

So in MongoDB you can store the data as nested subdocuments like so:

{    _id: acbdbd,    "property": "value",    "subdocumentproperty" : {        "param1": 1,        "param2": 2    }}

This data is only stored with the master document and your querying would be done through the master document, e.g.

db.collection.find({"subdocumentproperty.param1": 1})