Change field unique collection mongodb

4

Good morning,

I have a collection in mongodb with a unique:true field.

I would like to change this field to false.

If I change the code and delete the entire collection it works, but if I make the change in the code and do not delete the collection it does not accept.

My schema looks like this:

var chamadosSchema = new Schema({
    CODIGO_CONTRATO:{type:String, default:'', required:true},
    CODIGO_IMOVEL:{type:String, default:'', unique:true, required:true},
    ENDERECO:{type:String, default:'', required:true},
    BAIRRO:{type:String, default:'', required:true},
    PRIORIDADE:{type:String, default:'', required:true},
    NOME_PROPRIETARIO:{type:String, default:'', required:true},
    DDD_PROPRIETARIO:{type:String, default:''},
    TEL_RESIDENCIAL_PROPRIETARIO:{type:String, default:''},
    TEL_CELULAR_PROPRIETARIO:{type:String, default:''},
    TEL_COMERCIAL_PROPRIETARIO:{type:String, default:''},
    E_MAIL_PROPRIETARIO:{type:String, default:''},
    SOLICITANTE:{type:String, default:'', required:true},
    TEL_RESIDENCIAL_SOLICITANTE:{type:String, default:'', required:true},
    TEL_COMERCIAL_SOLICITANTE:{type:String, default:''},
    EMAIL_SOLICITANTE:{type:String, default:''},
    RP_CHAMADO:{type:String, default:'', required:true},
    DATA_CHAMADO:{type:Date, default:'', required:true},
    SOLICITACAO:{type:String, default:'', required:true}
});
    
asked by anonymous 27.02.2015 / 12:38

1 answer

2

This is the case of running db.collection.dropIndex . When you create this model with mongoose , it registers its collection and sets it to index the fields marked as unique . You can read more about how this works at the MongoDB level here .

The solution in your case is simple: run db.nomeDaCollection.dropIndex({ nomeDoCampo: 1 }) in the REGO of mongo (by running mongo to connect in your instance and use nomeDoSeuBanco to configure it to use your database.

In this case, I imagine your collection is called chamados , so:

$ mongo
MongoDB shell version: 2.6.7
connecting to: test
> db.chamados.dropIndex({ CODIGO_IMOVEL: 1 })
{ “nIndexesWas”: algum número, “ok”: 1 }
>
    
27.02.2015 / 16:56