Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

735
Visualizações
Is mongodb sub documents equivalent to Firestore subcollections?

For those of you who worked with Firebase (Firestore), then you can have a collection with documents in which each document has an id, then a collection can hold a sub collection (equivalent of an embedded document as an array of documents as a property).

Then, this sub collection can hold many documents while each has an id.

In Firestore the sub collection is lazy loaded.

It will fetch the documents on that collection, but if there is one or more sub collections, it won't retrieve it unless specifically going to that route. e.g: collection/document/subcollectionname/anotherdocument

So 2 questions:

  1. Are embedded documents lazy loaded? I don't want to get a document with all of its embedded documents (possibly a million) unless I explicitly access to it.
  2. How can I make sure each embedded document in MongoDB gets an "_id" in the form of ObjectID("blablabla")?

EDIT: I currently have a firestore implementation which has a subcollection/s practice behind it.

Example: organization => documentId => projects => projectId => activities => :activityType => activityId

  1. organization collection that holds documents (each document = organization).
  2. Each organization document holds a schema (id, name, language, etc..) and a few subcollections in which one of them is projects subcollection
  3. projects sub collection holds documents of projects.
  4. a project document holds the project schema (id, name, location, etc..) and one subcollection named activities.
  5. activities sub collection holds its own schema (id, type, category, etc...) and 6 more sub collections, each represents an activity type.
  6. each activity sub collection holds its own schema. No more sub collections.

Now, the good thing about it is that if I choose to get all organizations, then I will get only the documents of the organization collection and NOT the embedded subcollections (projects, etc..) while in MongoDB, I would get EVERYTHING per document.

How do I achieve the same nested documents with their own nested documents structure with the lazy load effect in MongoDB?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

How many activities can a single project have? If there's no limit then you're better off creating a root level collection for activities. In MongoDB, the maximum BSON document size is 16 MB. That being you may not be able to store all projects and their activities in a single document (organization document).

I would create 3 collections namely - organizations, projects and activities.

  1. Each organization should have a document in organizations collection similar to that you have in Firestore.
  2. Each project should have a document in projects collection containing a field "organizationID" so you can query projects of a specific organization using their ID. This is equivalent of a document in your projects sub-collection. Every project must also have it's own unique ID.
  3. Each activity should have a document in activities collection containing a field "projectID" so activities of a specific project can be retrieved.

enter image description here

I've added those additional organizationID, projectID fields even though you have _id just in case you'd like to have Firestore Document IDs there for easier side-by-side queries.

You don't have to worry about 16 MB document size limit this way and it'll be easier to query both projects and activities as long as you have the correct IDs.

Querying activities of a certain project:

await db.collection("activities").find({projectID: "myProjectID"}).toArray()

Thereafter it's upto you how you want to write queries with projections, aggregation, etc.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda