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

Make foo: { type: { name: String } } behave the same as { foo: new Schema({ name: String }) } #7181

Closed
vkarpov15 opened this issue Oct 25, 2018 · 4 comments
Labels
backwards-breaking discussion If you have any thoughts or comments on this issue, please share them!
Milestone

Comments

@vkarpov15
Copy link
Collaborator

Re: #7143. Right now the below declarations are different, and its a very subtle footgun that's hard to notice.

// `foo.name` is a string
new Schema({ foo: { name: String } });

// `foo` is mixed
new Schema({ foo: { type: { name: String } } });

Demo using Node shell:

$ node
> const { Schema } = require('mongoose')
undefined
> new Schema({ foo: { type: { name: String } } }).path('foo')
Mixed {
  path: 'foo',
  instance: 'Mixed',
  validators: [],
  setters: [],
  getters: [],
  options: { type: { name: [Function: String] } },
  _index: null,
  [Symbol(mongoose:schema_mixed)]: true }
> new Schema({ foo: { type: { name: String } } }).path('foo.name')
undefined
> 
> 
> new Schema({ foo: { name: String } }).path('foo')
undefined
> new Schema({ foo: { name: String } }).path('foo.name')
SchemaString {
  enumValues: [],
  regExp: null,
  path: 'foo.name',
  instance: 'String',
  validators: [],
  setters: [],
  getters: [],
  options: { type: [Function: String] },
  _index: null }
> 

Anybody have thoughts on this? I'm thinking it makes sense to deprecate this behavior, and make it so that new Schema({ foo: { type: { name: String } } }) makes foo.name a string in Mongoose 6.x.

@lineus @Fonger ?

@vkarpov15 vkarpov15 added discussion If you have any thoughts or comments on this issue, please share them! backwards-breaking labels Oct 25, 2018
@vkarpov15 vkarpov15 added this to the Parking Lot milestone Oct 25, 2018
@lineus
Copy link
Collaborator

lineus commented Oct 27, 2018

I agree with this change @vkarpov15. I can't think of a reason why anyone would create a new mixed path like this on purpose.

@mhombach
Copy link

Wait... so declaring a schema like in this example:
parent: [{ child1: { type: String, required: true }, child2: { type: String, required: true } }]
Is this a MIXED Type ???
When i want to use subdocuments and not mixed-type, do i need to declare a new schema for each subdocument? I am 90% sure that i read somewhere in the mongoose documenation that both ways are ok and do NOT create mixed-types...

@vkarpov15
Copy link
Collaborator Author

@mhombach no, in that example you get a document array. Examples:

// With below, `parent` will **not** be mixed or array of mixed
parent: { child1: { type: String, required: true }, child2: { type: String, required: true } }
parent: [{ type: { child1: { type: String, required: true }, child2: { type: String, required: true } } }]

// With the below, `parent` will be mixed
parent: { type: { child1: { type: String, required: true }, child2: { type: String, required: true } } }

In other words, this issue only impacts you if you declare a nested path using type or whatever your $typeKey is

@Fonger
Copy link
Contributor

Fonger commented Jan 24, 2019

This really makes sense especially for developers using TypeScript. This type definition is very common in TS so this can avoid confusion.

@vkarpov15 vkarpov15 modified the milestones: Parking Lot, 6.0 Dec 8, 2019
@vkarpov15 vkarpov15 changed the title Make foo: { type: { name: String } } behave the same as { foo: { name: String } } Make foo: { type: { name: String } } behave the same as { foo: new Schema({ name: String }) } Apr 25, 2020
vkarpov15 added a commit that referenced this issue Apr 26, 2020
…g }` create a single nested subdoc, remove `typePojoToMixed` because it is now always false

Fix #7181
vkarpov15 added a commit that referenced this issue Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards-breaking discussion If you have any thoughts or comments on this issue, please share them!
Projects
None yet
Development

No branches or pull requests

4 participants