1

Hello I had short time learn to mongoose so I had some question

I want to use two different schema in same collections because sometime I want to only repairshopnumber, sometime I want to arraydata at repairshopnumber

var infoSchema = new models.Schema({
    productnumber: {},
    repairshopnumber: {},
    reservationtimes: [],
    currenttime: Date,
    liftposition: Number,
    user_token: String

});
var ScheduleSchema = new models.Schema({
    productnumber: {},
    repairshopnumber: Number,
    reservationtimes: [],
    currenttime: Date,
    liftposition: Number,
    user_token: String

});
var reservationinfo = models.model('reservations', infoSchema);
var reservationschedule = mongoose.model('reservations', ScheduleSchema);

It's be occur the error

/home/node/node_modules/mongoose/lib/index.js:334
      throw new mongoose.Error.OverwriteModelError(name);
            ^
OverwriteModelError: Cannot overwrite `reservations` model once compiled.

How do I input that? I can't find any solution.

You.Brighton
  • 1,958
  • 2
  • 15
  • 22
  • You are writing the same model with two differente schemas. That's the problem. – michelem Oct 28 '15 at 07:38
  • @Michelem Mongoose actually supports multiple schemas for one collection: http://mongoosejs.com/docs/api.html#index_Mongoose-model . But the post owner used it incorrectly. – Frank Fang Jul 13 '16 at 08:30

1 Answers1

-2

It's no possible to define two schemas for your collection. The right things is to merge all of it in one schema.

In your schemas, the only difference is the field repairshopnumber. To use the array and the number type, you should define the type as Mixed :

var infoSchema = new models.Schema({
    productnumber: {},
    repairshopnumber: mongoose.Schema.Types.Mixed,
    reservationtimes: [],
    currenttime: Date,
    liftposition: Number,
    user_token: String
});

Warning : With this type, it is possible that number is stored in string

throrin19
  • 17,796
  • 4
  • 32
  • 52