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

Comment on: "manual/reference/operator/aggregation/size"

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • Server_Docs_20231030
    • None
    • manual, Server

    Description

      The example skips a very important caveat: All documents must have the list, otherwise there should be a match step before the size.

      example
      test collection:

      { "_id" : 1, "myArray" : [ 1, 2 ], "myStr" : "sfsaf"} { "_id" : 2, "myStr" : "asdf"}

      aggregation pipeline:
      db.getCollection('test').aggregate([{$project:{_id:1, "arraySize":{$size:"$myArray"}}}])

      error: uncaught exception: aggregate failed:

      { "ok" : 0, "errmsg" : "The argument to $size must be an array, but was of type: missing", "code" : 17124, "codeName" : "Location17124" }

      pipeline with match added:
      db.getCollection('test').aggregate([{$match:{myArray:{$exists:true}}},{$project:{_id:1, "arraySize":{$size:"$myArray"}}}])

      result:
      {
      "result" : [

      { "_id" : ObjectId("598b464d4fa2cd261a1cc433"), "arraySize" : 2 }

      ],
      "ok" : 1.0000000000000000
      }

      Attachments

        Activity

          People

            kay.kim@mongodb.com Kay Kim (Inactive)
            aaldredge@enspirecommerce.com Amy Aldredge
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              5 years, 8 weeks, 6 days ago