简体   繁体   中英

Mongoose Indexing

I am having problems with indexing in Mongoose. I have an employeeId field which is unique. When I create a employee document it creates a index "employeeId_1" and also I find there is a Index " id ". I would imagine it should be "_id". Could someone walk over what I am doing wrong? Here is the code:

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');     

Here is how I create the document.

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

Here is the getIndex results. For some reason I cannot recreate the issue but along with this there will be another index with the name employeeId_1. It will trow an duplicate error on key employeeId_1.

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

The _id field is created automatically for every document created. You can not change its name or remove it.

employeesSchema will has 2 primary keys:

  • employee._id
  • employee.employeeId

that is why you see two index in your mongoose


Edit:

After create model, we must wait for index event before create document

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

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

If you do not wait for index event, the index may not be created (and you will not see any warning from mongoose about it)

I have report this to mongoose and get reply on this issue: https://github.com/LearnBoost/mongoose/issues/1745

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM