Skip to content

Commit

Permalink
Merge pull request #13584 from Automattic/IslandRhythms/gh-13539
Browse files Browse the repository at this point in the history
`includeResultMetadata` option
  • Loading branch information
vkarpov15 committed Jul 7, 2023
2 parents 5f9593d + cec2ff4 commit e191580
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2435,7 +2435,7 @@ Query.prototype.collation = function(value) {
*/

Query.prototype._completeOne = function(doc, res, callback) {
if (!doc && !this.options.rawResult) {
if (!doc && !this.options.rawResult && !this.options.includeResultMetadata) {
return callback(null, null);
}

Expand Down Expand Up @@ -3099,7 +3099,7 @@ Query.prototype._deleteMany = async function _deleteMany() {
*/

function completeOne(model, doc, res, options, fields, userProvidedFields, pop, callback) {
if (options.rawResult && doc == null) {
if ((options.rawResult || options.includeResultMetadata) && doc == null) {
_init(null);
return null;
}
Expand All @@ -3112,7 +3112,7 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
}


if (options.rawResult) {
if (options.rawResult || options.includeResultMetadata) {
if (doc && casted) {
if (options.session != null) {
casted.$session(options.session);
Expand Down
15 changes: 15 additions & 0 deletions test/model.findOneAndUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2143,4 +2143,19 @@ describe('model: findOneAndUpdate:', function() {
assert.equal(doc.info['second.name'], 'Quiz');
assert.equal(doc.info2['second.name'], 'Quiz');
});
it('supports the `includeResultMetadata` option (gh-13539)', async function() {
const testSchema = new mongoose.Schema({
name: String
});
const Test = db.model('Test', testSchema);
await Test.create({
name: 'Test'
});
const doc = await Test.findOneAndUpdate({ name: 'Test' }, { name: 'Test Testerson' }, { new: true, upsert: true, includeResultMetadata: false });
assert.equal(doc.ok, undefined);
assert.equal(doc.name, 'Test Testerson');
const data = await Test.findOneAndUpdate({ name: 'Test Testerson' }, { name: 'Test' }, { new: true, upsert: true, includeResultMetadata: true });
assert(data.ok);
assert.equal(data.value.name, 'Test');
});
});

0 comments on commit e191580

Please sign in to comment.