Monday, 15 May 2017

Mongoose nested sub-document duplicate key error upon insert

Hello I'm using mongoosastic plugin (wrapper for elasticsearch.js) and mongoose.js. In a few words I have a model called Event which has children (nested sub-documents) declared as Game (model name). Further more Game has other children (nested sub documents) declared as Result (model name). So, Result is child of Game and Game is child of Event.

All of them have a property called nodeId which is unique indexed and required. There are no duplicate nodeIds, I have checked my data programatically so I'm wondering if I'm doing something wrong with my model declaration...

var ObjectSchema = new Schema({
    key: {
        unique: false,
        index: false,
        type: String,
        es_indexed: true
    },
    value: {
        unique: false,
        index: false,
        type: String,
        es_indexed: true
    }
});

var ResultSchema = new Schema({
    odd: {
        type: Number,
        index: true,
        es_indexed: true,
    },
    sortOrder: {
        type: Number,
        index: true,
        es_indexed: true,
    },
    specialOddsValue: {
        type: String,
        index: true,
        es_indexed: true
    },
    gameTypeResultsNr: {
        type: Number,
        index: true,
        es_indexed: true,
    },
    startDate: {
        type: Date,
        index: true,
        es_indexed: true,
        es_type: "date"
    },
    nodeId: {
        type: String,
        required: true,
        unique: true,
        index: true,
        es_indexed: true
    },
    parentNodeId: {
        type: String,
        required: true,
        index: true,
        es_indexed: true
    },
    localizedNames: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    },
    localizedAdditionalValues: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    },
    additionalValues: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    }
});

var GameSchema = new Schema({
    gameTypeId: {
        type: Number,
        index: true,
        es_indexed: true,
    },
    betTypeAvailability: {
        type: Number,
        index: true,
        es_indexed: true,
    },
    eventNodeTypeId: {
        type: Number,
        index: true,
        es_indexed: true,
    },
    startDate: {
        type: Date,
        index: true,
        es_indexed: true,
        es_type: "date"
    },
    nodeId: {
        type: String,
        required: true,
        unique: true,
        index: true,
        es_indexed: true
    },
    parentNodeId: {
        type: String,
        required: true,
        index: true,
        es_indexed: true
    },
    localizedNames: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    },
    localizedAdditionalValues: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    },
    additionalValues: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    },
    children: {
        type: [ResultSchema],
        index: false,
        es_indexed: true,
        es_type: "nested",
        es_include_in_parent: true
    },
    childrenCount: {
        type: Number,
        index: false,
        es_indexed: false,
    }
});

var EventSchema = new Schema({
    _id: ObjectId,
    eventNodeTypeId: {
        type: Number,
        index: true,
        es_indexed: true
    },
    status: {
        type: Number,
        index: true,
        es_indexed: true
    },
    startDate: {
        type: Date,
        index: true,
        es_indexed: true,
        es_type: "date"
    },
    _sport: {
        type: ObjectId,
        ref: "Sport",
        es_schema: SportSchema,
        es_indexed: true,
    },
    _league: {
        type: ObjectId,
        ref: "League",
        es_schema: LeagueSchema,
        es_indexed: true
    },
    _location: {
        type: ObjectId,
        ref: "Location",
        es_schema: LocationSchema,
        es_indexed: true
    },
    sportNodeId: {
        type: String,
        required: true,
        index: true,
        es_indexed: true
    },
    leagueNodeId: {
        type: String,
        required: true,
        index: true,
        es_indexed: true
    },
    locationNodeId: {
        type: String,
        required: true,
        index: true,
        es_indexed: true
    },
    nodeId: {
        type: String,
        required: true,
        unique: true,
        index: true,
        es_indexed: true
    },
    parentNodeId: {
        type: String,
        required: true,
        index: true,
        es_indexed: true
    },
    isActive: {
        type: Boolean,
        index: true,
        es_indexed: true
    },
    localizedNames: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    },
    localizedAdditionalValues: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    },
    additionalValues: {
        localizedValues: {
            type: [ObjectSchema],
            es_indexed: true,
            es_type: "nested",
            es_include_in_parent: true
        }
    },
    participants: {
        type: Array,
        index: true,
        es_indexed: true,
        es_type: "nested",
        es_include_in_parent: true
    },
    children: {
        type: [GameSchema],
        index: false,
        es_indexed: true,
        es_type: "nested",
        es_include_in_parent: true
    }
});

The error I'm getting upon import is the following:

{ MongoError: E11000 duplicate key error collection: opap.events index: children.nodeId_1 dup key: { : null }
    at Function.MongoError.create (D:\Development\xampp\htdocs\lab\praktorio\node-tools\importer\node_modules\mongodb-core\lib\error.js:31:11)
    at toError (D:\Development\xampp\htdocs\lab\praktorio\node-tools\importer\node_modules\mongodb\lib\utils.js:139:22)
    at D:\Development\xampp\htdocs\lab\praktorio\node-tools\importer\node_modules\mongodb\lib\collection.js:669:23
    at handleCallback (D:\Development\xampp\htdocs\lab\praktorio\node-tools\importer\node_modules\mongodb\lib\utils.js:120:56)
    at resultHandler (D:\Development\xampp\htdocs\lab\praktorio\node-tools\importer\node_modules\mongodb\lib\bulk\ordered.js:421:14)
    at D:\Development\xampp\htdocs\lab\praktorio\node-tools\importer\node_modules\mongodb-core\lib\connection\pool.js:461:18
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
  name: 'MongoError',
  message: 'E11000 duplicate key error collection: opap.events index: children.nodeId_1 dup key: { : null }',
  driver: true,
  code: 11000,
  index: 4,
  errmsg: 'E11000 duplicate key error collection: opap.events index: children.nodeId_1 dup key: { : null }',
  getOperation: [Function],
  toJSON: [Function],
  toString: [Function] }



via Syd

No comments:

Post a Comment