我做了一个虚拟对象,id是重复的,请检查我是否找到了如何用新的语法做这个,但找不到任何东西。有人能帮忙吗?
/**
* 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
我看过moongos的文档,并试图找到如何删除重复的,我看过旧的代码,但不工作的“ Mongoose ”:“^6.8.0”。如果有人知道如何删除第二个ID,我将不胜感激。谢谢
1条答案
按热度按时间yyhrrdl81#
这是因为
getters: true
同时包含了虚拟和路径getter。2考虑到这一点,你必须明确地禁用id
getter,就像mongoose文档中解释的那样:https://mongoosejs.com/docs/api.html#document_Document-id希望这能回答你的问题!