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

$meta : "textScore" returns null when used inside $group in aggregation and no other fields are needed

    • Fully Compatible
    • ALL
    • Hide
      db.text.drop()
      
      assert.writeOK(db.text.insert({ "_id" : 1, "title" : "cakes and ale" }))
      assert.writeOK(db.text.insert({ "_id" : 2, "title" : "more cakes" }))
      assert.writeOK(db.text.insert({ "_id" : 3, "title" : "bread" }))
      assert.writeOK(db.text.insert({ "_id" : 4, "title" : "some cakes" }))
      
      assert.commandWorked(db.text.createIndex({ title : "text" }))
      
      shellPrint(db.text.aggregate(
         [
           { $match: { $text: { $search: "cake" } } },
           { $group: { _id: { $meta: "textScore" }, count: { $sum: 1 } } }
         ]
      ))
      
      Show
      db.text.drop() assert .writeOK(db.text.insert({ "_id" : 1, "title" : "cakes and ale" })) assert .writeOK(db.text.insert({ "_id" : 2, "title" : "more cakes" })) assert .writeOK(db.text.insert({ "_id" : 3, "title" : "bread" })) assert .writeOK(db.text.insert({ "_id" : 4, "title" : "some cakes" })) assert .commandWorked(db.text.createIndex({ title : "text" })) shellPrint(db.text.aggregate( [ { $match: { $text: { $search: "cake" } } }, { $group: { _id: { $meta: "textScore" }, count: { $sum: 1 } } } ] ))
    • Query 2016-10-31
    • 0

      Our work to optimize $group to take advantage of COUNT_SCAN plans accidentally introduced this regression. Specifically, this change made it so that we will no longer request the text score from the query system if our only dependency is the text score.

      Original Description

      $meta : "textScore" returns null when used inside $group in aggregation under 3.3.15:

      > db.text.aggregate(
             [
               { $match: { $text: { $search: "cake" } } },
               { $group: { _id: { $meta: "textScore" }, count: { $sum: 1 } } }
             ]
          )
      
      { "_id" : null, "count" : 3 }
      

      Attached is a script repro.js that reproduces this problem.

      The behavior is correct on 3.2.10 and 3.0.12 where the same aggregation returns:

      { "_id" : 1, "count" : 2 }
      { "_id" : 0.75, "count" : 1 }
      

      If $meta : "textScore" is used in $project, it works fine:

      test> db.text.aggregate(
      ...    [
      ...      { $match: { $text: { $search: "cake" } } },
      ...      { $project : { score: { $meta: "textScore" }}}
      ...    ]
      ... )
      {
        "result": [
          {
            "_id": 1,
            "score": 0.75
          },
          {
            "_id": 2,
            "score": 1
          },
          {
            "_id": 4,
            "score": 1
          }
        ],
        "ok": 1
      }
      test>
      

      as well as if used in find():

      test> db.text.find({ $text : { $search : "cake" }},{ score: { $meta: "textScore" }})
      {
        "_id": 1,
        "title": "cakes and ale",
        "score": 0.75
      }
      {
        "_id": 2,
        "title": "more cakes",
        "score": 1
      }
      {
        "_id": 4,
        "title": "some cakes",
        "score": 1
      }
      Fetched 3 record(s) in 2ms
      test>
      

      The example is from https://docs.mongodb.com/v3.2/reference/operator/aggregation/meta/#examples

        1. repro.js
          0.5 kB
          Amar Hamzeh

            Assignee:
            charlie.swanson@mongodb.com Charlie Swanson
            Reporter:
            jaihirsch@carfax.com Jai Hirsch
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: