Skip to content

Commit 33211b5

Browse files
apkardongxinEric
authored andcommitted
It is legal to have index key specification to be simple string. (#124)
So far, we are expecting it to be object, Mongo shell can provide key spec as simple string for simple indexes.
1 parent 3e4e11a commit 33211b5

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/ExtMsg.actor.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,10 +753,28 @@ ACTOR Future<WriteCmdResult> attemptIndexInsertion(bson::BSONObj indexObj,
753753
Reference<ExtConnection> ec,
754754
Reference<DocTransaction> tr,
755755
Namespace ns) {
756-
757756
if (!indexObj.hasField(DocLayerConstants::NAME_FIELD))
758757
throw no_index_name();
759758

759+
// It is legal to have index key to be simple string for simple value indexes.
760+
if (indexObj.getField(DocLayerConstants::KEY_FIELD).isString()) {
761+
bson::BSONObjBuilder builder;
762+
for (auto it = indexObj.begin(); it.more();) {
763+
auto el = it.next();
764+
if (std::string(el.fieldName()) == DocLayerConstants::KEY_FIELD) {
765+
builder.append(DocLayerConstants::KEY_FIELD, BSON(el.String() << 1));
766+
} else {
767+
builder.append(el);
768+
}
769+
}
770+
indexObj = builder.obj();
771+
}
772+
773+
if (!indexObj.getField(DocLayerConstants::KEY_FIELD).isABSONObj()) {
774+
TraceEvent(SevWarn, "BadIndexSpec").detail("err_msg", "Bad key format");
775+
throw bad_index_specification();
776+
}
777+
760778
if (indexObj.getObjectField(DocLayerConstants::KEY_FIELD).nFields() == 1) {
761779
auto keyEl = indexObj.getObjectField(DocLayerConstants::KEY_FIELD).firstElement();
762780
if (!keyEl.isNumber() || !(keyEl.Number() == 1.0 || keyEl.Number() == -1.0)) {

0 commit comments

Comments
 (0)