Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs missing for ref and refPath as function #13834

Closed
1 task done
piyushk96 opened this issue Sep 7, 2023 · 3 comments · Fixed by #14133
Closed
1 task done

Docs missing for ref and refPath as function #13834

piyushk96 opened this issue Sep 7, 2023 · 3 comments · Fixed by #14133
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@piyushk96
Copy link

piyushk96 commented Sep 7, 2023

Prerequisites

  • I have written a descriptive issue title

Mongoose version

7.5.0

Node.js version

N/A

MongoDB version

N/A

Operating system

None

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

There is no proper documentation available on how to use a ref / refPath as functions in a schema.

Also, I want to understand what is the intended behaviour of these functions? Whether they are called only once during the schema initialization phase and cache the function return value or these functions are executed for each document.

The typescript mentions that these functions have access to this and doc so, is that mean that these functions will have access to document while processing and we can use document's fields to manipulate returned model or modelName
image

The only mention of ref function is here but it doesn't explains the above questions

Use Case:

const Entity1 = new Schema({
  f1: { type: Number, required: true},
});
const Entity1Model = mongoose.model('Entity1', Entity1);

const Entity2 = new Schema({
  f2: { type: Number, required: true},
});
const Entity2Model = mongoose.model('Entity2', Entity2);

const E3 = new Schema({
  itemType: { type: String, enum: ['e1', 'e2'], required: true},

  itemId: {
    type: Types.ObjectId,
    ref(doc) {
      return doc.itemType === 'e1' ? Entity1Model : Entity2Model;
      // OR
      // return this.itemType === 'e1' ? Entity1Model : Entity2Model;
    }
  }
})
const E3Model = mongoose.model('E3', E3);
@piyushk96 piyushk96 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Sep 7, 2023
@vkarpov15 vkarpov15 added this to the 7.5.4 milestone Sep 18, 2023
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Sep 18, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.5.4, 7.5.5 Oct 1, 2023
@piyushk96
Copy link
Author

@vkarpov15 can you please tell me the expected behavior here until the docs are released on the website

@vkarpov15 vkarpov15 modified the milestones: 7.6.2, 7.6.3 Oct 12, 2023
@vkarpov15
Copy link
Collaborator

ref and refPath functions are called every time you populate(), for every document being populated. Is that helpful @piyushk96 ?

@piyushk96
Copy link
Author

@vkarpov15 Thanks for the reply. Yes, this helps.

@vkarpov15 vkarpov15 modified the milestones: 7.6.6, 7.6.7 Nov 16, 2023
@IslandRhythms IslandRhythms linked a pull request Nov 30, 2023 that will close this issue
@vkarpov15 vkarpov15 modified the milestones: 7.6.7, 8.0.3 Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants