19
我想讓關鍵項目在該集合中唯一,但我不能得到這個工作,我在這裏發現類似的問題。Mongoose與模式密鑰重複唯一
task.js
function make(Schema, mongoose) {
var Tasks = new Schema({
project: { type: String, index: { unique: true, dropDups: true }},
description: String
});
mongoose.model('Task', Tasks);
}
module.exports.make = make;
test.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/rss');
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
require('./task.js').make(Schema, mongoose);
var Task = mongoose.model('Task');
var newTask = new Task({
project: 'Starting new project'
, description: 'New project in node'
});
newTask.save(function(err) {
if (err) console.log('Error on saving');
});
mongoose.disconnect();
當運行與節點test.js該應用程序,仍然產生重複。
MongoDB shell version: 2.0.2
connecting to: rss
> db.tasks.find()
> db.tasks.find()
{ "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa3d48d4e1533000001") }
{ "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa4d9a8921a33000001") }
{ "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa57ebeea1f33000001") }
//編輯還是同樣的問題,這裏就是我試圖做 刪除db.tasks.drop()集合 重啓蒙戈須藤停止的MongoDB,並開始MongoDB的,再試,還是運行程序同樣的問題,它如何允許索引上的重複數據?
dropDups來自mongodb-native – jackdbernier
dropDups從2.6版棄用https://docs.mongodb.com/v2.6/tutorial/create-a-unique-index/#drop-duplicates –