$elematch quiz(mongoDB basics week 3)


This is the systax so far but so confused ,can someone explain it.

Hi @sai_18604,

Please check for the following things:

  • The placement of brackets is wrong in the query. You can skip using square brackets in the query as you are already using curly brackets {}.

  • The spelling of $elemMatch operator is wrong.

  • Since you are doing an equality condition on score and product, enclose the both conditions in a single curly bracket.

Please let me know, if you are still facing any problem.

Thanks,
Sonali

Its working!thanks for that

The same problem is with this filtering


Using an ‘or’ operator to filter the scores gte 70 and lte 80 but its not working

Hi @sai_18604,

Please check for the following things:

  • You are searching for scores gte 70 and lte 80 but you haven’t mentioned score in the query.

  • Skip using square brackets as you are already using curly brackets.

  • You do not need to use $or operator because query itself says “Filter scores gte 70 and lte 80”.

You can refer following docs to get a better understanding about forming queries:

Query Array
Query Array of Embedded Documents

For any other doubt, please feel free to reply.

Thanks,
Sonali

but scores is the collection I guess,results is the field

Hi @sai_18604,

This solution will work if you are searching for results.score in the surveys collection.

If you are searching for results gte 70 and lte 80 in the scores collection, I would again recommend to follow these steps:

  • Skip using square brackets as you are already using curly brackets.

  • Do not use $or operator as you want values in results array to be gte 70 and lte 80 (Note that it will return all those documents where atleast one value in results array matches the query filter).

You can refer to the following doc more such queries:
Query an Array

I hope this solves you issue.

Thanks,
Sonali