Mongoose中的Mongoose错误E11000重复键错误索引

时间:2015-07-23 07:37:37

标签: node.js mongodb mongoose mongodb-query

我正在使用MongooseSails。我正在创建自己的ACL,其中我的架构由processName及其subprocesses组成,其中subprocessName分配了不同的permissions,即READ },WRITEADMINNA

我无法为我的数据库中的不同subprocess保存相同的processName。当我尝试执行下面给出的相同操作时,我收到以下错误

  "name": "MongoError",
  "message": "E11000 duplicate key error index:ACL.processnames.$subprocess.subprocessName_1 dup key: { : \"aaaa\" }",
  "index": 0,
  "code": 11000,
  "errmsg": "E11000 duplicate key error index: ACL.processnames.$subprocess.subprocessName_1 dup key: { : \"aaaa\" }"

我的Schema如下

{
 processName: {
        type: String,
        required: true,
        unique: true
    },

    subprocess: [{
        subprocessName: {type:String},
        permission: {
            type: String,
            enum: ["READ", "WRITE", "ADMIN","NA"],
            default: "NA"
        }
    }]
}

我使用Node Insepector并发现我的代码在.save函数执行期间失败,如下所示

var db = require('../services/db.js'); //DB Connections
var ProcessName = mongoose.model('ProcessName');
var data = {};
data.processName = processName;
data.subprocess = subprocess;
var processes = db.ProcessName(data);
processes.save(processes, function (err, data) {
                if (err) {
                    return res.json({
                        sucess: false,
                        exception: err
                    });
                }
                else {
                return res.json({
                        sucess: true,
                        processDetail: data
                    }); 
                }      
            });

我的样本数据在DHC上发布时遇到上述错误,如下所示

  {
       "processName":"wessssess LIkes",
       "subprocess":[
                      {"subprocessName":"ssas","permission":"ADMIN"},
                      {"subprocessName":"aaaa","permission":"ADMIN"},
                      {"subprocessName":"aay"}
       ]
  }

有人可以帮我解决这个问题吗?

0 个答案:

没有答案