我将我的Schema设计为具有唯一字段。
mNotificationManager.cancel(_notificationId);
}
由于我正在删除索引的一些更改,我的新设计看起来像。
var bookingSchema = new Schema({
booking_id_customer: {
type: Number,
index: { unique: true }
}
但我仍然从mongodb获得重复键错误索引消息。当我提供重复的bookingId时。 我尝试了reIndex()方法。
var bookingSchema = new Schema({
booking_id_customer: {
type: Number
}
}
但我得到booking.reIndex不是函数错误。 我的mongoDb托管在mlabs中。 如何克服这个问题。谢谢。
答案 0 :(得分:0)
首先,您需要删除索引,然后在完成后重新索引字段,所以这样做(ShopModel在这里是对我的集合或模型ShopModel的引用,有时也称为db.whateverYourCollectionNameIs):
const ShopModel = require('./shop_model');
ShopModel.collection.dropIndexes(function(){
ShopModel.collection.reIndex(function(finished){
console.log("finished re indexing")
})
})
要检查索引是否正常运行,可以运行:
ShopModel.collection.getIndexes({full: true}).then(indexes => {
console.log("indexes:", indexes);
}).catch(console.error);