【发布时间】:2017-06-27 16:19:54
【问题描述】:
好的,经过大量试验和错误后,我确定当我删除一个集合然后通过我的应用程序重新创建它时,唯一性在我重新启动本地节点服务器之前不起作用。这是我的架构
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Services = new Schema ({
type : {type : String},
subscriptionInfo : Schema.Types.Mixed,
data : Schema.Types.Mixed
},{_id:false});
var Hashtags = new Schema ({
name: {type : String},
services : [Services]
},{_id:false});
var SubscriptionSchema = new Schema ({
eventId : {type: String, index: { unique: true, dropDups: true }},
hashtags : [Hashtags]
});
module.exports = mongoose.model('Subscription', SubscriptionSchema);
这是我的路线……
router.route('/')
.post(function(req, res) {
var subscription = new subscribeModel();
subscription.eventId = eventId;
subscription.save(function(err, subscription) {
if (err)
res.send(err);
else
res.json({
message: subscription
});
});
})
如果我删除集合,然后点击上面看到的 /subscribe 端点,它将创建条目但不会尊重重复项。直到我重新启动服务器,它才开始兑现它。任何想法为什么会这样?谢谢!
【问题讨论】:
标签: javascript node.js mongodb mongoose mongodb-query