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

175
Visualizações
I could not query with different condition in mongoose

This is my Operator Models:

const operatorSchema = new Schema({
    operatorName: {
        type: String
    },

    users:[{
        email:String,
        payment:Number,
        paymentsData: Date,
        product: String,

      }],


});

I need to filter by operatorName and email in users block. But when I try with this I get all users in related OperatorName how can I query correctly ?

 Operators.find( { $and: [{operatorName: operatorName},  {'users.email': 'super@m.com'}]}, function (err, docs) {
      ) {

    if (err) console.log(err)
    else {
        docs.forEach(function(data){ 
        console.log(data)
         })
        // res.render('total_earns_operator_tables', { operators: docs });
    } 
  }); 

EDIT

I also try with aggregate method like this but again, I get same result and I gel all bunch of user data, but I want only demouser@mail.com

  Operators.aggregate([
        { $match: {$and: [{ operatorName: operatorName},{'users.email': 
         'demouser@mail.com' }]}},
        ]
    ,function (err, docs) {   
        // console.log(Operators)
        //  Operators.find( { $and: [{operatorName: operatorName},  {users: {$elemMatch: {email:['super@m.com']}}}]}, function (err, docs) {
            // Operators.find( {operatorName: operatorName,  "users.email": "demouser@mail.com"}, function (err, docs) {

        if (err) console.log(err)
        else {
            docs.forEach(function(data){ 
            console.log(data)
             })
            // res.render('total_earns_operator_tables', { operators: docs });
        } 
      }); 

It is very basic but I couldnt find solution.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your query is doing exactly what it is supposed to do. It is returning all documents that satisfy you two criteria: 1. having a specified operatorName and 2. users array having at least one user matching the specified email.

If you want to reashape your documents by filtering the user array to only include the user matching your condition, you'll have to use an aggregation.

EDIT

As per your edit: Your aggregation only have a $match stage, which is identical to your query above. To change the shape of a document, the aggregation framework provides you with the $project stage, see the example below:

Operators.aggregate([
  {
    $match: {
      operatorName: operatorName,
      "users.email": "demouser@mail.com"
    }
  },
  {
    $project: {
      operatorName: '$operatorName',
      users: {
        $filter: {
          input: "$users",
          as: "user",
          cond: {
            $eq: [
              "$$user.email",
              "demouser@mail.com"
            ]
          }
        }
      }
    }
  }
]

Here, we first filter the collection to get only the documents you want, using the $match stage, then we use the $filteroperator in $project stage, to return only the matching users within the array.

See the working playground

about 4 years ago · Juan Pablo Isaza 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