Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

367
Views
Mongoose no asigna automáticamente un _id a los elementos de mi matriz cuando los presiona, ¿por qué?

entonces, como dice el título, quiero que mongoose agregue automáticamente un campo _id a los objetos que inserto en mi matriz.

Aquí está mi esquema de mangosta:

 var playerModel = new Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: "Users", }, class: { type: String, required: true, }, level: { type: Number, default: 1, }, spells: { type: [String], default: ["", "", "", "", ""], }, inventory: [], toolbelt: { axe: [{}], pickaxe: [{}], fishingrod: [{}], hammer: [{}], saw: [{}], sewingkit: [{}], knife: [{}], gemkit: [{}], food: [{}], potion: [{}], }, gear: { weapon: [{}], head: [{}], chest: [{}], gloves: [{}], legs: [{}], shoes: [{}], earrings: [{}], ring: [{}], necklace: [{}], }, gold: { type: Number, default: 0, }, bagSize: { type: Number, default: 20, }, skills: { type: [userSkills], default: defaultSkills, }, });

Entonces, en última instancia, cuando envío un nuevo elemento a mi player.inventory, quiero que mongoose agregue automáticamente el _id, pero no lo hace. También en mi esquema, defino el inventario para que sea solo una matriz para poder insertar diferentes objetos que no tienen la misma estructura.

Así que cada vez que hago esto

 player.inventory.push(player.toolbelt.pickaxe[0]);

Quiero que mi elemento tenga todas sus propiedades de lo que inserté y también una propiedad _id generada automáticamente.

Ahora se ve así en mi base de datos: Imagen de mi campo Mongo DB

Espero haberme aclarado y alguien me pueda ayudar :)

¡Gracias a todos!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Para cualquiera en el futuro, solucioné mi problema creando un nuevo esquema con la opción estricta establecida en falso:

 var inventoryItem = new Schema({}, { strict: false });

y luego decir que el inventario es:

 inventory: [inventorySchema],

Por alguna razón esto funciona, pero no esto:

 inventory: [{}, { strict: false }],

tan bueno saber! :)

Buenos días !

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!