Change field Unique Collection mongodb

Asked

Viewed 421 times

4

Good morning,

I have a Collection in the mongodb with a field unique:true.

I would like to change this field to false.

If I change the code and delete all Collection works, but if I make the change in the code and do not delete the Collection he doesn’t accept.

My schema is 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}
});

1 answer

2


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

The solution in your case is simple: turn db.nomeDaCollection.dropIndex({ nomeDoCampo: 1 }) in the REPL of Mongo (running mongo to connect to your instance and use nomeDoSeuBanco to configure you to use your database.

In that case, I’m guessing your name is Ollection chamados, therefore:

$ mongo
MongoDB shell version: 2.6.7
connecting to: test
> db.chamados.dropIndex({ CODIGO_IMOVEL: 1 })
{ “nIndexesWas”: algum número, “ok”: 1 }
>
  • Thank you very much yamadapc, it worked.

  • Don’t forget to accept the answer if your problem has been solved :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.