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

183
Views
Mongoose agrega parentdoc si no existe O agrega subdoc si existe el padre pero el subdoc no

Básicamente, tengo un usuario que tiene un correo electrónico y una lista de suscripciones. Quiero crear el Usuario con la suscripción si el usuario no existe, si el usuario existe pero no tiene la suscripción Quiero simplemente agregar la nueva suscripción a la lista de suscripciones de Usuarios. Finalmente, si tanto el usuario como la suscripción ya existen, quiero manejar la suscripción existente a mi manera.

 await UserDocument.findOneAndUpdate( {'$and': [{ 'email': email, }, { 'subscriptions.product': product-id, }]}, {'$setOnInsert': newCustomerWithSubscription, '$push': {'email.$.subscription': newSubscription}}, {upsert: true, new: false}, async function(err: CallbackError, doc: UserSchemaType | null) { //Code to handle if both exists }).exec();

Diseño de base de datos:

 User: { email, subscriptions: [{ //sub-info }] }
over 4 years ago · Santiago Trujillo
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!