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

in 4.4, $meta: "textScore" projection in findAndModify only appears if you include in both sort and projection

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.7.0
    • Affects Version/s: None
    • Component/s: None
    • Fully Compatible
    • Query 2020-05-18
    • 44

      You can only project the text score as part of findAndModify if you also specify the sort option in findAndModify.
      If you specify only the projection, it does not return anything.

      To reproduce: In v4.4.0-rc2-11-gc3b47f6

      1. db.articles.insertMany([
           { "_id" : 1, "title" : "cakes and ale" },
           { "_id" : 2, "title" : "more cakes" },
           { "_id" : 3, "title" : "bread" },
           { "_id" : 4, "title" : "some cakes" },
           { "_id" : 5, "title" : "two cakes to go" },
           { "_id" : 6, "title" : "pie" }
        ]);
        db.articles.createIndex( { title: "text"} );
        
      2. Try projection only
        db.articles.findAndModify( { query: { $text: { $search: "cake" }  },  fields:  { score: { $meta: "textScore"}  },   update: { $set: { x: "foo" } } } );
        

        This does not include the textScore

        { "_id" : 4, "title" : "some cakes" }
        
      3. Try with sort
        db.articles.findAndModify( { query: { $text: { $search: "cake" }  },  fields:  { score: { $meta: "textScore"}  },   sort: { bar: { $meta: "textScore"} }, update: { $set: { x: "foo" } } } );
        

        And the text score is projected

        { "_id" : 2, "title" : "more cakes", "score" : 1 }
        


      In 4.2, you weren't allowed to use $meta in sort as part of findAndModify because it would error mistakenly state that you don't have projection for the meta field (when you do) and if you specify the projection only, it'd just return 0. So, it does appear that it probably never quite worked with findAndModify.  

      MongoDB Enterprise > db.articles.findAndModify( { query: { $text: { $search: "cake" }  },  fields:  { score: { $meta: "textScore"}  },   update: { $set: { x: "foo" } } } );
      { "_id" : 4, "title" : "some cakes", "score" : 0 }
      MongoDB Enterprise > db.articles.findAndModify( { query: { $text: { $search: "cake" }  },  fields:  { score: { $meta: "textScore"}  },   sort: { bar: { $meta: "textScore"} }, update: { $set: { x: "foo" } } } );
      2020-04-27T13:01:34.253-0400 E  QUERY    [js] uncaught exception: Error: findAndModifyFailed failed: {
      	"ok" : 0,
      	"errmsg" : "must have $meta projection for all $meta sort keys",
      	"code" : 2,
      	"codeName" : "BadValue"
      } :
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:725:15
      @(shell):1:1
      

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            kay.kim@mongodb.com Kay Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: