Open timheerwagen opened 1 week ago
I'm not sure I understand what the question is here. How are you creating your models?
One of my models:
const MongooseFileSchema = new mongoose.Schema<FileSchema>(
{
name: { type: String, required: true },
},
);
MongooseFileSchema.index({
name: "text",
});
export const FileModel =
(mongoose.models?.[FILE_MODEL_NAME] as Model<FileSchema>) ||
mongoose.model<FileSchema>(FILE_MODEL_NAME, MongooseFileSchema);
If this model is not yet used by FileModel.find, it is not included in mongoose.models
.
Prerequisites
Mongoose version
8.7.0
Node.js version
20.14.10
MongoDB version
7.x
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
11
Issue
For a backup solution i'm restoring mongodb data from a json file.
In case that a model is not yet initialized yet, I offer fallback solutions for insertMany.
Is there a way to initialize all defined models directly during creation so that the data can be parsed correctly by the schema? This is useful if the model can't be defined in a list and is not yet in use. (e.g. plugins) Otherwise I have to come up with some serialization for deeply nested ObjectId's or Dates.