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

303
Views
Cómo almacenar una matriz vacía en mongodb

Quiero almacenar una matriz vacía en la clave de amigos del esquema de usuario. Pero mangosta no me deja agregar una serie vacía de amigos.

Aquí está mi esquema de usuario

 const userSchema = mongoose.Schema({ firstName: { type: String, required: true, }, lastName: { type: String, required: true, }, userName: { type: String, unique: true, required: true, }, email: { type: String, required: true, unique: true, validate: [validator.isEmail, "Please enter a valid Email"], }, password: { type: String, required: true, minlength: [6, "Password must be at least 6 characters"], }, friends: [ { type: mongoose.Schema.Types.ObjectId, ref: 'User' unique: true, }, ], });

Y aquí está mi código para extraer datos de una matriz y guardar una matriz vacía

 const index = req.user.friends.findIndex(friend => friend.toString() === req.params.id); if(index !== -1){ req.user.friends.splice(index, 1); }else{ req.user.friends.push(req.params.id); } const user = new User(req.user); await user.save({validateBeforeSave: false}); return res.json({success: true});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

especifique el valor predeterminado en su lugar

 friends: [ { type: mongoose.Schema.Types.ObjectId, ref: 'User' unique: true, default: [] }, ],
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!