Home > other >  Duplicated ID's in Mongoose
Duplicated ID's in Mongoose

Time:12-21

I have made a virtuals object and the id is duplicated have check if I find how to do this with the new syntax and can't find any thing. Can someone help?

/**
 * Import only the mongoose in this class
 * This plugin is for MongoDB and this class decide how the Mongo document should be for the Tour objects
 */
import mongoose from 'mongoose';
import slugify from 'slugify';
/**
 * Config the Tour object what name and the type of attributes
 */
const tourSchema = new mongoose.Schema([
  {
    name: {
      type: String,
      required: [true, 'A tour must have a name'],
      unique: true,
      trim: true,
    },
    slug: String,
    duration: {
      type: Number,
      required: [true, 'A tour must have a duration'],
    },
    maxGroupSize: {
      type: Number,
      required: [true, 'A tour must have a max group size'],
    },
    difficulty: {
      type: String,
      required: [true, 'A tour must have a difficulty'],
    },
    ratingsAverage: {
      type: Number,
      default: 4.5,
    },

    ratingsQuantity: {
      type: Number,
      default: 0,
    },

    price: {
      type: Number,
      required: [true, 'A tour must have a price'],
    },

    priceDiscount: Number,

    summary: {
      type: String,
      trim: true,
      required: [true, 'A tour must have a summary'],
    },
    description: {
      type: String,
      trim: true,
    },
    imageCover: {
      type: String,
      trim: true,
      required: [true, 'A tour must have an cover image'],
    },
    images: [String],

    createAt: {
      type: Date,
      default: Date.now,
      select: false,
    },
    startDates: [Date],
  },
]);
tourSchema.set('toJSON', { getters: true, virtuals: true});
tourSchema.set('toObject', { getters: true, virtuals: true });

tourSchema.virtual('durationWeeks').get(function () {
  return this.duration / 7;
});

// tourSchema.pre('save', function (next) {
//   this.slug = slugify(this.name, { lower: true });
//   next();
// });

// tourSchema.post('save', function(doc,next){
//   console.log(doc);
//   next();
// });


/**
 * Create a mogoose module this puts in the name and type of attributes Tour object should have and what must be writend and what is optional
 */
const Tour = mongoose.model('Tours', tourSchema);
/**
 * Export the Tour constant
 */
export default Tour;

The Object from MongoDB

I have look at the moongos documentation and try to find how to remove the duplicated and I have look at older code but that does not work with "mongoose": "^6.8.0". If there are someone that knows how to remove the second ID I will be grateful. Thankes

CodePudding user response:

this happens because the getters: true includes both virtual and path getters. Given this you have to explicty disable the id getter as explained on the mongoose documentation: https://mongoosejs.com/docs/api.html#document_Document-id

new Schema({ name: String }, { id: false });

Hope this answers your quesiton!

  • Related