[SERVER-47801] in 4.4, $meta: "textScore" projection in findAndModify only appears if you include in both sort and projection Created: 27/Apr/20  Updated: 29/Oct/23  Resolved: 11/May/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 4.7.0

Type: Bug Priority: Major - P3
Reporter: Kay Kim (Inactive) Assignee: David Storch
Resolution: Fixed Votes: 0
Labels: qexec-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to SERVER-48368 fts_find_and_modify.js should be blac... Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 2020-05-18
Participants:
Linked BF Score: 44

 Description   

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



 Comments   
Comment by Githook User [ 19/May/20 ]

Author:

{'name': 'David Storch', 'email': 'david.storch@mongodb.com', 'username': 'dstorch'}

Message: SERVER-47801 Blacklist fts_find_and_modify.js from multiversion passthroughs
Branch: master
https://github.com/mongodb/mongo/commit/956b43ff33c8c3c3c98cc0dc18fd439b54d29394

Comment by Githook User [ 11/May/20 ]

Author:

{'name': 'David Storch', 'email': 'david.storch@mongodb.com', 'username': 'dstorch'}

Message: SERVER-47801 Fix $meta:"textScore" for findAndModify
Branch: master
https://github.com/mongodb/mongo/commit/72f7cde3240e4bab161e1b60b57451c793dbd428

Generated at Thu Feb 08 05:15:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.