Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-14134

Positional projection operator fails if no query is specified for field

    XMLWordPrintableJSON

Details

    Description

      Our documentation says that positional projection operator returns The first element if no query condition is specified for the array (Starting in MongoDB 4.4).

      It looks like we do not have such behaviour implemented:

      $ mongod --version
      db version v4.4.1
      Build Info: {
          "version": "4.4.1",
          "gitVersion": "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1",
          "modules": [],
          "allocator": "system",
          "environment": {
              "distarch": "x86_64",
              "target_arch": "x86_64"
          }
      }
       
      > db.test.find()
      { "_id" : ObjectId("5ff47546d0e2d9910975e0cb"), "x" : 1, "a" : [ 1, 2, 3 ] }
       
      > db.test.find({}, {'a.$': 1})
      Error: error: {
      	"ok" : 0,
      	"errmsg" : "positional operator '.$' couldn't find a matching element in the array",
      	"code" : 51246,
      	"codeName" : "Location51246"
      }
       
      > db.test.find({x: 1}, {'a.$': 1})
      Error: error: {
      	"ok" : 0,
      	"errmsg" : "positional operator '.$' couldn't find a matching element in the array",
      	"code" : 51246,
      	"codeName" : "Location51246"
      }
      

      In queries above we do not specify query condition for the array. The query fails instead of returning the first element of array in field a.

      Attachments

        Activity

          People

            andrew.feierabend@mongodb.com Andrew Feierabend (Inactive)
            nikita.lapkov@mongodb.com Nikita Lapkov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              2 years, 49 weeks, 1 day ago