summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMickey. J Winters <mickey.winters@mongodb.com>2023-11-13 20:02:43 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-11-13 20:28:17 +0000
commit3367195a14d0ba2734d2ba2719294fb974ad0834 (patch)
tree3c136b864ac953ecf486fbe0e748e84e8a86655c
parent2c0bf6dafaa926d16f095f0ff91bedaa7f5b0863 (diff)
SERVER-83150 make Document::shred() copy document metadatar5.0.23-rc0r5.0.23
-rw-r--r--jstests/aggregation/sources/shred_documents.js21
-rw-r--r--src/mongo/db/exec/document_value/document.cpp1
-rw-r--r--src/mongo/db/exec/document_value/document_value_test.cpp11
3 files changed, 32 insertions, 1 deletions
diff --git a/jstests/aggregation/sources/shred_documents.js b/jstests/aggregation/sources/shred_documents.js
index d7b0aa35d1f..dd5d16c4e00 100644
--- a/jstests/aggregation/sources/shred_documents.js
+++ b/jstests/aggregation/sources/shred_documents.js
@@ -1,5 +1,9 @@
/**
* Test $_internalShredDocuments parsing and make sure it doesn't modify documents.
+ * this test assumes {$meta: "indexKey"} will not be missing.
+ * @tags: [
+ * do_not_wrap_aggregations_in_facets
+ * ]
*/
"use strict";
@@ -8,7 +12,8 @@ load("jstests/aggregation/extras/utils.js");
(function() {
const coll = db[jsTestName()];
coll.insertMany(
- [{a: 1, obj: {a: 1}, arr: [{a: 1}]}, {a: 1, obj: {a: 1}, arr: [{a: 1}]}, {}, {a: 1}]);
+ [{a: 1, obj: {a: 1}, arr: [{a: 1}]}, {a: 2, obj: {a: 1}, arr: [{a: 1}]}, {}, {a: 3}]);
+coll.createIndex({a: 1});
assert.commandFailedWithCode(assert.throws(() => coll.aggregate({$_internalShredDocuments: 1})),
7997500);
assert.commandFailedWithCode(
@@ -39,4 +44,18 @@ assertNoop(matchExcludeGroup, 0);
assertNoop(matchExcludeGroup, 1);
assertNoop(matchExcludeGroup, 2);
assertNoop(matchExcludeGroup, 3);
+
+assertNoop([{$match: {a: 1}}, {$addFields: {key: {$meta: "indexKey"}}}], 1);
+
+// The shred() function is also used by set windowFields so lets test that too.
+const res = coll.aggregate([
+ {$match: {a: 1}},
+ {$addFields: {key: {$meta: "indexKey"}}},
+ {$setWindowFields: {sortBy: {a: 1}, output: {w: {$rank: {}}}}},
+ {$limit: 1}
+ ])
+ .toArray();
+assert.eq(1, res.length);
+assert(res[0].hasOwnProperty("key"));
+assert.eq({a: 1}, res[0]["key"]);
})();
diff --git a/src/mongo/db/exec/document_value/document.cpp b/src/mongo/db/exec/document_value/document.cpp
index e905dec2e70..80a6cfa5fd8 100644
--- a/src/mongo/db/exec/document_value/document.cpp
+++ b/src/mongo/db/exec/document_value/document.cpp
@@ -377,6 +377,7 @@ Document DocumentStorage::shred() const {
md[it.fieldName()] = valueElem.val.shred();
}
}
+ md.setMetadata(DocumentMetadataFields(metadata()));
return md.freeze();
}
diff --git a/src/mongo/db/exec/document_value/document_value_test.cpp b/src/mongo/db/exec/document_value/document_value_test.cpp
index 411a8b473ed..5260853e5bf 100644
--- a/src/mongo/db/exec/document_value/document_value_test.cpp
+++ b/src/mongo/db/exec/document_value/document_value_test.cpp
@@ -387,6 +387,17 @@ TEST(ShredDocument, HandlesModifiedDocuments) {
ASSERT(!shredded["subObj"]["b"].missing());
}
+TEST(ShredDocument, HandlesMetadata) {
+ BSONObj bson = BSON("a" << 1 << "subObj" << BSON("a" << 1));
+ Document original = fromBson(bson);
+ MutableDocument md(original);
+ DocumentMetadataFields meta;
+ meta.setSearchScore(6);
+ md.setMetadata(std::move(meta));
+ Document shredded = md.freeze().shred();
+ ASSERT_EQ(6, shredded.metadata().getSearchScore());
+}
+
/** Add Document fields. */
class AddField {
public: