0

Is it good practice and or recommended to reference 2 documents to each other using ref? Like this:

const FirstDocumentId = mongoose.model(
    'FirstDocumentId',
    new mongoose.Schema({
        coolField: String,
        secondDocumentId: {type: Mongoose.Schema.Types.ObjectId, ref: 'secondDocumentId'}
    })
);


const SecondDocumentId = mongoose.model(
    'SecondDocumentId',
    new mongoose.Schema({
        someField: String,
        firstDocumentId: {type: Mongoose.Schema.Types.ObjectId, ref: 'firstDocumentId'}
    })
);
1

All depends on their relationship. However, the way you describe it implies that this is a 1-to-1 relationship, since each schema refers to the other. Therefore, why not just combine the schemas?

  • Thanks for the answer Chris! The reason I can't do that is cuz they will both contain a lot of fields and I won't always need all fields from SecondDocumentId when accessing FirstDocumentId and vice versa – Jessica Feb 19 at 4:46
  • That is a common concern ... what do you mean "a lot of fields" ... we have documents with 200+ fields. – Chris Edgington Feb 20 at 4:00
  • I don't have anywhere near 200 fields. But the less fields I have the faster it is to read/update/write. And I think it keeps things more organized. Why is this way worse? – Jessica Feb 20 at 5:24
  • You are introducing unnecessary complexity. What do you mean "the faster it is to read/update/write" - mongodb is incredibly fast, and document field could is not the primary impacter of the time for operations. Are you basing this on what you think is the case or did you do an analysis of CRUD operations with small-field-quantity and large-field-quantity documents? – Chris Edgington Feb 20 at 5:42
  • If you want it to be "organized" - put what you would have put in the second document in a sub-object within the first document. In other words ... you can make all the secondary document fields properties of a single first-level property in the first document. – Chris Edgington Feb 20 at 5:44
0

The relationship between the two collections is one-to-one. Lets consider the two collections as:

first_coll:

_id: ObjectId,
theId: <string>,
descr: <string>,
// and other fields

second_coll:

_id: ObjectId,
theId: <string>,
name: <string>,
// and other fields

The field theId, is defined for both the collections, and have the same value. This field establishes the relationship between the collections. And, this field value need to be a unique for each document; this means both collections will have same number of documents.

When you want to query the first_coll or second_coll individually, use the theId field or any other field (like the _id) for accessing the documents.

When you want to query the first_coll as primary collection, but get some details from the second collection, then you can do a join (left outer join) aggregation $lookup query and shape the result document as per your need. Similarly with the second_coll.

Identifying what are the primary queries and how they are formed in the application will give better insight to look into aspects and refining the design further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.