【发布时间】:2021-09-02 19:56:59
【问题描述】:
MongoError: E11000 duplicate key error collection: myFirstDatabase.tours index: rating_1 dup key: { rating: null }
当我使用邮递员创建数据时,我不明白这个错误,说 dup key: {rating:null},shcema 中没有任何评级键,这是从哪里来的?为什么我只能创建一次。
这是我的代码(tourSchema)
const mongoose = require('mongoose');
const tourSchema = new mongoose.Schema({
name:{
type:String,
required:[true, 'tour must have name'],
unique:true,
trim: true
},
ratingAverage: Number,
ratingQty:Number,
discount:Number,
price:{
type:String,
required:[true, 'tour must have price']
},
summary:{
type:String,
required:[true, 'tour must have summary'],
trim:true
},
description:{
type:String,
trim:true
},
imageCover:{
type:String,
required:[true, 'tour must have imageCover'],
trim:true
},
images: [String],
createdAt:{
type:Date,
default:Date.now()
},
startDate:[Date]
})
const Tour = mongoose.model('Tour', tourSchema);
module.exports = Tour;
这是我正在尝试添加的数据
{
"name":"the mountain explorer",
"ratingAverage":4,
"ratingQty":1,
"price":400,
"summary":"this is mountain explorer near to solua, best known for the river around it",
"description":"this mountain is located at the top of siraha mountain, has so many rivers around it",
"imageCover":"nepal_tour_cover-1.jpg",
"images":["nepal_tour-1.jpg", "nepal_tour-2.jpg", "nepal_tour-3.jpg"],
"startDate":["2020-03-12,12:44", "2020-04-12,12:54"]
}
【问题讨论】:
标签: node.js mongodb mongoose backend