Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-15918

Unable to create field with update which is calculated with size array.

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.6.4
    • Component/s: Querying
    • None

      Hi,

      I've a document which contains an array of nested object.
      I need to have the global sum of the size of array.
      I've written an agrgegation:

      db.values.aggregate( 
          [
              {$group : 	{
                              _id     : null , 
                           	count	:   { 
                                              $sum: {$size : '$markers'}
                                          }
                                      }
              } 
          ])
      

      My collection contains 2610537 documents, so the query is very long. (Too long for me)

      I think it will be quicker to have an intermadiate field with the markers size. But I cannot write the update query:

      db.values.update(
          { 'sample' : 1753068}, // Used to reduce documents and test.
          { 
              '$set': {
                  'count': { $size : markers}
              } 
          },
          {
              upsert : false
          }
      )
      

      This give me this error:
      ReferenceError: markers is not defined (shell):5

      But 'markers' is my arrays of nested objects. so this field exist.

      After reading the doc, $size is not part of update operators...

      Is there a way to handle this case? (I know , I can do this on update or insert process)

      Thanks

      Mickael

            Assignee:
            ramon.fernandez@mongodb.com Ramon Fernandez Marina
            Reporter:
            mgervais GERVAIS Mickaël
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: