Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-1828

Unable to apply logical operators inside elemMatch

    • Type: Icon: Bug Bug
    • Resolution: Community Answered
    • Priority: Icon: Major - P3 Major - P3
    • No version
    • Affects Version/s: 2.2.4
    • Component/s: None
    • Environment:
      OS:
      node.js / npm versions:
      Additional info:
    • Not Needed
    • Developer Tools

      Problem Statement

      Consider the collection sales

      [
        {     _id: ObjectId('668ea197ccaa7da04b117b7c'),     results: [ 82, 85, 88 ]   },
        {     _id: ObjectId('668ea197ccaa7da04b117b7d'),     results: [ 75, 88, 89 ]   }
      ]

      Q: Find all the documents where there contains at least one element in array result such that : 80<=ele<85

      Implicit version used:
      db.scores.find({ results: { $elemMatch:

      { $lt: 85, $gte: 80 }

      } })

      Explicit Version ($and) used
      db.scores.find({ results: { $elemMatch: { $and: [

      { $lt: 85 }

      , { $gte: 80 }] } } })

       

      Error:MongoServerError[BadValue]: unknown top level operator: $lt. If you have a field name that starts with a '$' symbol, consider using $getField or $setField.

       

      We can't do something like below

      db.scores.find({ $and: [ { results: { $elemMatch:

      { $gte: 80 }

      } }, { results: { $elemMatch:

      { $lt: 85 }

      } }] })

      as it will give both the records

        1. mongoDB_Shell_Version.png
          39 kB
          Garima Sharma
        2. shell_output.png
          58 kB
          Garima Sharma

            Assignee:
            Unassigned Unassigned
            Reporter:
            templatecreator00@gmail.com Garima Sharma
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: