Home > database >  I could not query with different condition in mongoose
I could not query with different condition in mongoose

Time:08-08

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': '[email protected]'}]}, 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 [email protected]

  Operators.aggregate([
        { $match: {$and: [{ operatorName: operatorName},{'users.email': 
         '[email protected]' }]}},
        ]
    ,function (err, docs) {   
        // console.log(Operators)
        //  Operators.find( { $and: [{operatorName: operatorName},  {users: {$elemMatch: {email:['[email protected]']}}}]}, function (err, docs) {
            // Operators.find( {operatorName: operatorName,  "users.email": "[email protected]"}, 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.

CodePudding user response:

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": "[email protected]"
    }
  },
  {
    $project: {
      operatorName: '$operatorName',
      users: {
        $filter: {
          input: "$users",
          as: "user",
          cond: {
            $eq: [
              "$$user.email",
              "[email protected]"
            ]
          }
        }
      }
    }
  }
]

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

  • Related