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

171
Views
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 answers
Answer question

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 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!