具有唯一索引约束的mongoimport

时间:2019-05-07 10:36:04

标签: node.js mongodb mongoose

下面是用于将任何数据推入数据库中的模式,即每次创建新条目时

var ContainerSchema= new Schema({
    powderName: {type: String},
    powderCssColor: {type: String},
    batchName: {type: String},
    batchCssColor: {type: String},
    containerType: {type: String},
    mass: {type: Number},
    containerEmptyWeight: {type: Number},
    capacity: {type: Number},
    bookedForSource: {type: Boolean},
    bookedForDest: {type: Boolean},
    selectionTimestamp: {type: Number},
    selectedByUserId: {type: String},
    containerOrMachineName: {type: String},
    isSeived: {type: Boolean},
    isMachine: {type: Boolean},
    lastSeiveCycle: {type: Number},
    supplierName: {type: String},
    machineDetails: {
        serialNumber: {
            type: String,
            index: {unique: true, dropDups: true}
        },
        sapNumber: {
            type: String,
            index: {unique: true, dropDups: true}
        },
        deliveryDate: {type: String},
        status: {type: String}
    },
    mData : { type: Schema.Types.ObjectId, ref: 'Machine_Type_Schema'}
});

现在,我已有一些现有数据,这些数据是使用以下mongoimport命令导入的,

mongoimport --db powdermanagement --collection container_schemas --type csv --fields "powderName.string(),powderCssColor.string(),batchName.string(),batchCssColor.string(),containerOrMachineName.string(),isSeived.boolean(),isMachine.boolean(),lastSeiveCycle.int32(),containerType.string(),mass.double(),bookedForSource.boolean(),bookedForDest.boolean(),selectionTimestamp.int32(),selectedByUserId.string(),capacity.int32(),supplierName.string()" --columnsHaveTypes --file "C:\Users\p\Desktop\containersMapping_Updated.csv"

该命令成功执行并且插入了数据,但是对于任何新条目,我仍然可以插入重复的serialNumbersapNumber数据, 我不要。

  

因此,我可以将唯一性约束作为此查询的一部分,以便   对于导入的数据,此唯一性很明显,或者请在   如果我做错了。

0 个答案:

没有答案