Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda