-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
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
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"} );
- 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" }
- 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
- related to
-
SERVER-48368 fts_find_and_modify.js should be blacklisted in passthroughs that require retryable writes
- Closed