2016-08-19 2 views
0
var estadoSchema = Schema({ 
    estado_nome: String 
}); 

var cidadeSchema = Schema({ 
    cidade_nome: String, 
    estado_reference: [{ type: Schema.Types.ObjectId, ref: 'Estados'}] 
}); 

이 디자인 스키마, 그리고 데이터베이스에 포함 아래, 나는이 문제를 삽입 시간에 반복, 이상이 존재하지 않는 경우에만 schma 상태를 만들 수있을 것이라고 스키마 상태에 관한 한, 스키마 도시를 참조로 상태와 연관시킵니다. 스키마 생성. estadoSchemaestado_nome에서몽구스가 채울

Estado.create(req.body) 
      .then(function(estado){ 
       Cidade.create({cidade_nome: cidade, cidade_reference: [estado._id]}) 
         .then(function(cidade){ 
          res.status(201).json(cidade); 
         }, function(erro){ 
          res.status.json(erro) 
         }); 
       res.status(201).json(estado); 
      }, function(erro){ 
       res.status.json(erro) 
      }); 

답변

0

시도 unique : true. 이렇게하면 새로운 상태가 존재하지 않고 중복이없는 경우에만 새 상태가 만들어집니다.

var estadoSchema = Schema({ 
    estado_nome: { 
     type :String, 
     unique : true 
    } 
}); 
관련 문제