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

203
Views
mangosta Consultando matriz de documentos internos

Tengo un diseño de esquema como este

 var userNotificationSchema = new Schema({ notification_id: { type: Schema.Types.ObjectId, ref: 'notifications' }, isRead: { type: Boolean } }); var userSchema = new Schema({ notification: [userNotificationSchema] });

Quiero obtener toda la lista de matrices de notificaciones que isRead: 'false' .

Para eso escribí

 Model.User.find({ _id: userid, 'notification.isRead': false }, function (err, result) { console.log(result); res.send(result); });

pero esto devuelve [] como resultado.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

puede probarlo usando aggregate si desea obtener solo aquellas notificaciones que tienen el campo isRead como false .

 Model.User.aggregate([ {$match:{_id: userid}}, {$unwind:"$notification"}, {$match:{"notification.isRead": false}}, {$group:{_id:"$_id",notification:{ $addToSet: "$notification"}}} ]).exec(function (err, result) { console.log(result); res.send(result); })

Por ejemplo, su documento como:

 { "_id" : ObjectId("58454926668bde730a460e15"), "notification" : [ { "notification_id" : ObjectId("58454926668bde730a460e16"), "isRead" : true }, { "notification_id" : ObjectId("58454926668bde730a460e17"), "isRead" : true }, { "notification_id" : ObjectId("58454926668bde730a460e19"), "isRead" : false } ] }

entonces la salida será como:

 { "_id" : ObjectId("58454926668bde730a460e15"), "notification" : [ { "notification_id" : ObjectId("58454926668bde730a460e19"), "isReady" : false } ] }

Si desea obtener todas las notificaciones si alguno de isRead es false , entonces su consulta es correcta, simplemente verifique que el ID de userid exista en la base de datos que pasó y alguna notificación isRead es falsa. También puede usar $elemMatch

 Model.User.find({ _id: userid "notification":{ $elemMatch: { "isRead": false} } }).exec(function (err, result) { console.log(result); res.send(result); })
over 4 years ago · Santiago Trujillo Report

0

Supongo que su referencia es incorrecta porque la consulta es correcta.

Asegúrese de estar refiriendo lo que está exportando.

Ejemplo:

Si está refiriendo notifications en su código de esquema, debe exportar el mismo nombre en el código.

 module.exports = mongoose.model("notifications", userNotificationSchema);

Eche un vistazo aquí https://alexanderzeitler.com/articles/mongoose-referencing-schema-in-properties-and-arrays/

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!