summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Rassi <rassi@10gen.com>2013-12-19 21:16:42 -0500
committerJason Rassi <rassi@10gen.com>2013-12-19 21:16:42 -0500
commit815bb1efe0f5c3f8ae6c5f0eccf6cf96e4936ab8 (patch)
treee11e789876ef6884724ed7a42f8bebe41688ace7
parent9ceadd4ece53d98762967b27bb897d09d912c1c8 (diff)
downloadmongo-815bb1efe0f5c3f8ae6c5f0eccf6cf96e4936ab8.tar.gz
SERVER-12092 mongorestore should generate valid "create" command obj
Fixes an issue where fields in the "create" command object generated by mongorestore could be out of order, such that an invalid command would be run instead of "create". Conflicts: src/mongo/tools/restore.cpp
-rw-r--r--src/mongo/tools/restore.cpp41
1 files changed, 18 insertions, 23 deletions
diff --git a/src/mongo/tools/restore.cpp b/src/mongo/tools/restore.cpp
index abd7f868afe..3563c715641 100644
--- a/src/mongo/tools/restore.cpp
+++ b/src/mongo/tools/restore.cpp
@@ -484,34 +484,29 @@ private:
return nfields == obj2.nFields();
}
- void createCollectionWithOptions(BSONObj cmdObj) {
+ void createCollectionWithOptions(BSONObj obj) {
+ BSONObjIterator i(obj);
- // Create a new cmdObj to skip undefined fields and fix collection name
+ // Rebuild obj as a command object for the "create" command.
+ // - {create: <name>} comes first, where <name> is the new name for the collection
+ // - elements with type Undefined get skipped over
BSONObjBuilder bo;
-
- // Add a "create" field if it doesn't exist
- if (!cmdObj.hasField("create")) {
- bo.append("create", _curcoll);
- }
-
- BSONObjIterator i(cmdObj);
- while ( i.more() ) {
+ bo.append("create", _curcoll);
+ while (i.more()) {
BSONElement e = i.next();
- // Replace the "create" field with the name of the collection we are actually creating
if (strcmp(e.fieldName(), "create") == 0) {
- bo.append("create", _curcoll);
+ continue;
}
- else {
- if (e.type() == Undefined) {
- log() << _curns << ": skipping undefined field: " << e.fieldName() << endl;
- }
- else {
- bo.append(e);
- }
+
+ if (e.type() == Undefined) {
+ log() << _curns << ": skipping undefined field: " << e.fieldName() << endl;
+ continue;
}
+
+ bo.append(e);
}
- cmdObj = bo.obj();
+ obj = bo.obj();
BSONObj fields = BSON("options" << 1);
scoped_ptr<DBClientCursor> cursor(conn().query(_curdb + ".system.namespaces", Query(BSON("name" << _curns)), 0, 0, &fields));
@@ -520,7 +515,7 @@ private:
if (cursor->more()) {
createColl = false;
BSONObj obj = cursor->next();
- if (!obj.hasField("options") || !optionsSame(cmdObj, obj["options"].Obj())) {
+ if (!obj.hasField("options") || !optionsSame(obj, obj["options"].Obj())) {
log() << "WARNING: collection " << _curns << " exists with different options than are in the metadata.json file and not using --drop. Options in the metadata file will be ignored." << endl;
}
}
@@ -530,10 +525,10 @@ private:
}
BSONObj info;
- if (!conn().runCommand(_curdb, cmdObj, info)) {
+ if (!conn().runCommand(_curdb, obj, info)) {
uasserted(15936, "Creating collection " + _curns + " failed. Errmsg: " + info["errmsg"].String());
} else {
- log() << "\tCreated collection " << _curns << " with options: " << cmdObj.jsonString() << endl;
+ log() << "\tCreated collection " << _curns << " with options: " << obj.jsonString() << endl;
}
}