summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/index_entry_comparison.cpp
diff options
context:
space:
mode:
authorXiangyu Yao <xiangyu.yao@mongodb.com>2018-09-13 11:03:19 -0400
committerXiangyu Yao <xiangyu.yao@mongodb.com>2018-09-14 11:04:50 -0400
commit2072abc1824e633bb0b0fdf330fb40fe410d5092 (patch)
treef51ea6d28a62f5ab9886a2d0448a6049ebf2e1d3 /src/mongo/db/storage/index_entry_comparison.cpp
parent913f15287664b8513876735f6f6198e212ad4e17 (diff)
downloadmongo-2072abc1824e633bb0b0fdf330fb40fe410d5092.tar.gz
SERVER-14801 Add field names to duplicate key error messages
Diffstat (limited to 'src/mongo/db/storage/index_entry_comparison.cpp')
-rw-r--r--src/mongo/db/storage/index_entry_comparison.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/mongo/db/storage/index_entry_comparison.cpp b/src/mongo/db/storage/index_entry_comparison.cpp
index 41a1ae709e9..b87fcf4f354 100644
--- a/src/mongo/db/storage/index_entry_comparison.cpp
+++ b/src/mongo/db/storage/index_entry_comparison.cpp
@@ -164,4 +164,34 @@ BSONObj IndexEntryComparison::makeQueryObject(const BSONObj& keyPrefix,
return bb.obj();
}
+Status buildDupKeyErrorStatus(const BSONObj& key,
+ const std::string& collectionNamespace,
+ const std::string& indexName,
+ const BSONObj& keyPattern) {
+ StringBuilder sb;
+ sb << "E11000 duplicate key error";
+ sb << " collection: " << collectionNamespace;
+ sb << " index: " << indexName;
+ sb << " dup key: ";
+
+ BSONObjBuilder builder;
+ // key is a document with forms like: '{ : 123}', '{ : {num: 123} }', '{ : 123, : "str" }'
+ BSONObjIterator keyValueIt(key);
+ // keyPattern is a document with only one level. e.g. '{a : 1, b : -1}', '{a.b : 1}'
+ BSONObjIterator keyNameIt(keyPattern);
+ // Combine key and keyPattern into one document which represents a mapping from indexFieldName
+ // to indexKey.
+ while (1) {
+ BSONElement keyValueElem = keyValueIt.next();
+ BSONElement keyNameElem = keyNameIt.next();
+ if (keyNameElem.eoo())
+ break;
+
+ builder.appendAs(keyValueElem, keyNameElem.fieldName());
+ }
+
+ sb << builder.obj();
+ return Status(ErrorCodes::DuplicateKey, sb.str());
+}
+
} // namespace mongo