0

I have two separate pipelines which output the same answer. I'm trying to understand why you would use $size in the projection stage. I don't quite understand what the $size operator is doing in this pipeline.

[
  {
    '$project': {
      'title': {
         $size: {
           '$split': [
               '$title', ' '
            ]
        }
      }
    }
  }, {
    '$match': {
      'title': {
        '$eq': 1
      }
    }
  }, {
    '$count': 'title'
  }
]

I rewrote the pipeline to use $size in the $match stage instead and the output is the same. Is there some algorithmic penalty on either of these?

[
  {
    '$project': {
      'title': {
        '$split': [
          '$title', ' '
        ]
      }
    }
  }, {
    '$match': {
      'title': {
        '$size': 1
      }
    }
  }, {
    '$count': 'title'
  }
]
Jeremy Fiel
  • 140
  • 10

1 Answers1

0

I am trying to solve your query.

$size counts and returns the total the number of items in an array and argument for $size must resolve to an array.

  { 
    // "title" : "hello world"
    // { '$split': [ '$title', ' ' ] } -> [ "hello",  "world"]
    // { { $size:': .... }} -> 2
    '$project': { 'title': { $size: { '$split': [ '$title', ' ' ] } } }
  }, 
  {
    // Here you want to match whose title is equal to 2
    '$match': { 'title': { '$eq': 2 } }
  }

$split convert string divides a string into an array of substrings based on a second argument in array.

In second pipline query

  { 
    // "title" : "hello world"
    // { '$split': [ '$title', ' ' ] } -> [ "hello",  "world"]
    // {  title: [ "hello",  "world"] }
    '$project': { 'title': { '$split': [ '$title', ' ' ]}}}
  },
  { 
    // Here you want to match whose title size is 2
    '$match': { 'title': { '$size': 2 }}
  } 

Both query are same with differenct praspective. In your case title: "hello" so it return same result.

Ashok
  • 2,846
  • 1
  • 12
  • 20