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

Ghost collections auto-created for each subdocument #11498

Closed
sandorvasas opened this issue Mar 8, 2022 · 7 comments
Closed

Ghost collections auto-created for each subdocument #11498

sandorvasas opened this issue Mar 8, 2022 · 7 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@sandorvasas
Copy link

sandorvasas commented Mar 8, 2022

Do you want to request a feature or report a bug?
BUG

What is the current behavior?
I don't know since when this is happening, I just noticed recently that empty collections were created in our DB. one collection for each subdocument in the schema. For example, if we have a schema like

export const PawSchema = new mongoose.Schema({
  size: Number,
  temperature: Number
});
export const PawModel = mongoose.model('Paw', PawSchema)

export const DoggoSchema = new mongoose.Schema({
  chonkiness: Number,
  paws: [PawSchema]
}, {
  timestamps: {
    createdAt: 'createdAt',
    updatedAt: 'updatedAt',
  },
  collection: 'doggos',
});
export const DoggoModel = mongoose.model('Doggo', DoggoSchema)

Results in two automatically created collections: paws and doggos. This had not been happening before. collection above is only defined for doggos, so it should only create doggos, as before.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose 6.2.1
Node 16.14
MongoDB 4.4.12

@sandorvasas
Copy link
Author

Can be disabled with mongoose.set('autoCreate', false)

@sandorvasas
Copy link
Author

Thanks.

@sandorvasas
Copy link
Author

np

@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 9, 2022

Actually when do you use PawModel?

@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 9, 2022

I assume you never actually need PawModel, so you can delete PawModel.

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Mar 9, 2022
@sandorvasas
Copy link
Author

sandorvasas commented Mar 21, 2022

It actually didn't work. Ghost collections are still created all around. I even tried setting { autoCreate: false } on all the schemas.
I do need PawModel for example when pushing new PawModel({...}) into another model's nested array, but that's not the point here. I just want to be able to make it work again as it had been working before.

@vkarpov15
Copy link
Collaborator

@sandorvasas you don't need PawModel if all you're doing is pushing onto another model's nested array. In general, if you don't need a collection, you don't need a model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

4 participants