Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
mongoose Querying inner document array

I have a schema design like this

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

var userSchema = new Schema({
    notification: [userNotificationSchema]
});

I want to fetch all notifications array list which isRead: 'false'.

For that I wrote

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

but this returns [] as result.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

you can try it using aggregate if you want to get only those notifications that has isRead field is 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);
})

For example your document like:

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

then out put will be like:

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

If you want to get all notifications if any one of isRead is false then your query is correct just check userid is is exist in DB that you passed and some notification isRead is false . Also can use $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 Relatório

0

My guess is your reference is wrong because the query is correct.

Make sure you are referring what you exporting.

Example:

If you are referring notifications in your schema code then you should export the same name in code.

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

Take a look at here https://alexanderzeitler.com/articles/mongoose-referencing-schema-in-properties-and-arrays/

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda