2013-12-14 7 views
1

몽구스 색인 생성에 문제가 있습니다. 고유 한 employeeId 필드가 있습니다. 직원 문서를 만들면 "employeeId_1"색인이 생성되고 색인 "id"이 있습니다. 나는 그것이 "_id"이어야한다고 생각한다. 누군가 내가 뭘 잘못하고 있는지 걸을 수 있습니까? 다음은 코드입니다.몽구스 색인 생성

var mongoose = require('mongoose'); 
var ObjectId = mongoose.SchemaTypes.ObjectId; 

var eContactSchema = mongoose.Schema({ 
    contacttype:{type: Number}, 
    name:{type: String, required:true}, 
    phone:{type: String, trim: true}, 
    mail1:{type: String, trim: true}, 
    mail2:{type: String, trim: true}, 
    city:{type: String, trim: true}, 
    state:{type: String, trim: true}, 
    zip:{type: String, trim: true} 
}) 

var eCourseSchema = mongoose.Schema({ 
    course:{type: ObjectId, required:true, unique: true}, 
    title:{type: String}, 
    notes:{type: String, trim:true}, 
    expire:{type: Date}, 
    deleted: {type: Boolean, default: false} 
}) 

var eAppSchema = mongoose.Schema({ 
    appName:{type: String, required: true, trim: true}, 
    view:{type: Boolean, default: false}, 
    create:{type: Boolean,default: false}, 
    edit:{type: Boolean,default: false}, 
    del:{type: Boolean,default: false} 
}) 

var employeesSchema = mongoose.Schema({ 
    firstName:{type: String, trim:true, required:true}, 
    lastName:{type: String, trim:true, required:true}, 
    employeeId: {type: String, trim:true, required:true, unique: true}, 
    active: {type: Boolean, default: true}, 
    admin: {type: Boolean, default: false}, 
    title: {type: String, trim:true}, 
    pin:{type:String, trim: true}, 
    contacts:[eContactSchema], 
    courses:[eCourseSchema], 
    apps:[eAppSchema] 
}) 

module.exports = db.model('employees', employeesSchema,'employees');  

다음은 문서 작성 방법입니다.

 exports.addEmployee = function (data, callback) { 
    employees.create(data, function (err) { callback(err) }); 
}; 

다음은 getIndex 결과입니다. 어떤 이유로 나는이 문제를 재현 할 수 없지만 이것과 함께 employeeId_1이라는 이름의 또 다른 색인이있을 것입니다. employeeId_1 키에 중복 오류가 발생합니다.

[ 
    { 
    "v": 1, 
    "key": { 
     "_id":1 
    }, 
    "ns": "Management.employees", 
    "name": "_id_" 
    } 
] 
+1

합니다. 쉘에서'db.employees.getIndexes()'출력을 포함하도록 질문을 편집 할 수 있습니까? – JohnnyHK

답변

0

생성 된 모든 문서에 대해 _id 필드가 자동으로 생성됩니다. 이름을 변경하거나 제거 할 수 없습니다.

employeesSchema의 의지는이 기본 키를 가지고 :

  • 당신이 당신의 몽구스에 두 개의 인덱스를 참조하는 이유

을 employee.employeeId employee._id


수정 :

모델을 생성 한 후, 우리는 당신이 인덱스 이벤트, 인덱스가 만들어지지 않을 수 있습니다 기다릴 수없는 경우 이전 문서를

User = mongoose.model('users', UserSchema); 

User.on('index', function() { 
    new User({}).save(); 
    new User({}).save(); 
    new User({}).save(); 
    new User({}).save(); 
}) 

을 만들 인덱스 이벤트를 기다려야합니다 (당신은 그것에 대해 몽구스에서 경고가 표시되지 않습니다)

나는 이것이 몽구스와이 문제에 대한 답변을 얻을 수있는 보고서가 : 당신이 요구하는지 모르겠어요 https://github.com/LearnBoost/mongoose/issues/1745

+0

이제 두 번째 색인 employeeId도 얻지 못했습니다. –

+0

답변이 업데이트되었습니다! – damphat

+0

on ('index')이 문제라면, db 모델 생성을위한 install.js와 문서 추가를위한 app.js를 만들고, 터미널에서 별도로 실행할 수 있습니다. – damphat