Skip to content
Discussion options

You must be logged in to vote

This is expected behavior due to Mongoose's strict mode (enabled by default).

When you access doc.myField via dot notation, Mongoose only exposes paths defined in your schema. Paths not in the schema return undefined. However, doc.get("myField") uses an internal getter that reads directly from the underlying document data, bypassing schema restrictions — which is why it works.

Solutions:

1. Add the field to your schema (recommended):

const schema = new Schema({ myField: Schema.Types.Mixed });

2. Disable strict mode (allows any field to be stored and accessed):

const schema = new Schema({ ... }, { strict: false });

3. Convert to a plain object first:

const plain = doc.toObject();
console.log(

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by AbdelrahmanHafez
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants