1
votes

I'm trying to dump/restore a mongo collection that has validators. These validators have regexes, which, on the surface, it looks like mongorestore cannot import: First, I create my collection:

use test
db.users.drop()
db.createCollection("users", {validator : {
                                    name : {
                                      $type : "string",
                                      $regex : /^[A-z]*$/
                                    },
                                  }
                    })
// { "ok" : 1 }
db.getCollectionInfos()
// looks like it should...
db.users.insertOne({"name": "inv@lid"});
// fails
db.users.insertOne({"name": "Valid"});
// succeeds
db.users.find()
// { "_id" : ObjectId("59cd85d84f2803b08e9218ac"), "name" : "Valid" }

Then, I run a dump, which seems fine:

/usr/bin/mongodump --host $MYHOST \
                    --port $MYPORT \
                    --db test \
                    --gzip \
                    --archive=test.mongodump.gz

Then, a restore, which fails:

/usr/bin/mongorestore --host $MYHOST \
                      --port $MYPORT \
                      --gzip \
                      --archive=test.mongodump.gz

Error:

2017-09-28T23:31:30.626+0000    preparing collections to restore from
2017-09-28T23:31:30.691+0000    reading metadata for test.users from archive 'test.mongodump.gz'
2017-09-28T23:31:30.692+0000    Failed: test.users: error parsing metadata from archive 'test.mongodump.gz': extended json in 'options': expected $regex field to have string value

I've poured over the docs to mongdump and mongorestore, but haven't really gotten anywhere. I have tried --noOptionsRestore, same error.

I'm relatively new to mongo, so I could just be missing something simple...

1

1 Answers

2
votes

It turns out that, if you have a $regex, you also needs $options, even if $options = ''. The error message sort of hints at this, but very poorly. Changing the create statement from:

db.createCollection("users", {validator : {
                                    name : {
                                      $type : "string",
                                      $regex : /^[A-z]*$/
                                    },
                                  }
                    })

to

db.createCollection("users", {validator : {
                                    name : {
                                      $type : "string",
                                      $regex : /^[A-z]*$/,
                                      $options: ''
                                    },
                                  }
                    })

solves the problem