'Unable to get result from $match with $and in pymongo

I have mongodb query which I am trying to implement with pymongo.

I have the collection dictionaries as below:

{'_id': 0,
 'name': 'aimee Zank',
 'scores': [{'score': 10.463179736705023, 'type': 'exam'},
  {'score': 11.78273309957772, 'type': 'quiz'},
  {'score': 75.8740349954354, 'type': 'homework'}]}


{'_id': 1,
 'name': 'Tomas Jude',
 'scores': [{'score': 55.9736705023, 'type': 'exam'},
  {'score': 50.78273309957772, 'type': 'quiz'},
  {'score': 45.8740349954354, 'type': 'homework'}]}

I am trying to query for students with score higher than 40 in all three types (exam, quiz and homework). For this, I am using $match with $and in the aggregate. I am unable to get any result, with $or the condition works correctly.

agg_result=Collection.aggregate([
    {"$unwind" : "$scores" },
   {"$match": {"scores.score": {"$gt":40}}},
   { 
       "$match": {
           "$and" : [
                     {"scores.type": "exam"},
                     {"scores.type":"homework"}
                     ]
                  }
    },
   
   {
       "$group": {
           "_id" : "$_id",
           "name": {"$first": "$name"},
          "scores":{"$push" : "$scores"}
       }
   },
   
  {
      "$sort": {
          "_id" : 1
      }
  }

])

With $or, the result shows as,

{'_id': 0,
     'name': 'aimee Zank',
     'scores': [{'score': 75.8740349954354, 'type': 'homework'}]}

{'_id': 1,
     'name': 'Tomas Jude',
     'scores': [{'score': 55.9736705023, 'type': 'exam'},
      {'score': 45.8740349954354, 'type': 'homework'}]}

How to work around for $and?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source