Collections - mongodb

Asked

Viewed 221 times

8

I own a collection called suspeitosSchema and another call acoesSchema.

suspectsSchema:

const suspeitosSchema = new mongoose.Schema({
  sexo: { type: String },
  etnia: { type: String },
  cumprimentoCabelo: { type: String },
  corCabelo: { type: String },
  altura: { type: String },
  peso: { type: String },
  tipoArma: { type: String },
  armaBranca: { type: String },
  armaDeFogo: { type: String },
  observacao: { type: String }
})

acoesSchema:

const acoesSchema = new mongoose.Schema({
  dataCadastro: { type: Date, default: Date.now },
  fonte: { type: Array, required: true },/*
  bo: { type: String },*/
  numeroBo: { type: Number, required: true },
  imagem: { type: String },
  relato: { type: String, required: true },
  modus: { type: String, required: true },
  falhasApuradas: { type: String, required: true },
  data: { type: Date, required: true }, 
  latitude: { type: String, default: '-27.226520' },
  longitude: { type: String, default: '-52.018375' },
  suspeitos: [suspeitosSchema],
  veiculos: [veiculosSchema],
  tipoAcao: { type: String, required: true, required: true }
})

Note that in the collection acoes has a field called suspeitos, in this way, I can store from a form the data of a suspect along with the rest of the data.

However, I need to add more than one suspect per form, I’m not able to do this, does anyone have any idea how I could do?

  • I think iterating the number of suspects and adding to an array before storing in acoesSchema ai stores this array ;)

1 answer

6


Correct for: suspeitos: [{ type: ObjectId, ref: 'suspeitosSchema' }]

const acoesSchema = new mongoose.Schema({
  dataCadastro: { type: Date, default: Date.now },
  fonte: { type: Array, required: true },/*
  bo: { type: String },*/
  numeroBo: { type: Number, required: true },
  imagem: { type: String },
  relato: { type: String, required: true },
  modus: { type: String, required: true },
  falhasApuradas: { type: String, required: true },
  data: { type: Date, required: true }, 
  latitude: { type: String, default: '-27.226520' },
  longitude: { type: String, default: '-52.018375' },
  suspeitos: [{ type: ObjectId, ref: 'suspeitosSchema' }],
  veiculos: [{ type: ObjectId, ref: 'veiculosSchema' }],
  tipoAcao: { type: String, required: true, required: true }
})
  • agree with @Hudsonph’s reply, if you define that it is an array of Objectid’s you can then use populate to show and, to save in the bank only send the objectid’s in action.

Browser other questions tagged

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