【问题标题】:Can mongorestore handle regex validators?mongorestore 可以处理正则表达式验证器吗?
【发布时间】:2017-10-10 06:54:13
【问题描述】:

我正在尝试转储/恢复具有验证器的 mongo 集合。这些验证器有正则表达式,从表面上看,mongorestore 似乎无法导入: 首先,我创建我的收藏:

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" }

然后,我运行一个转储,这似乎很好:

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

然后,还原失败:

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

错误:

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

我已将文档倾注到 mongorestore 和 mongorestore,但还没有真正得到任何结果。我试过--noOptionsRestore,同样的错误。

我对 mongo 比较陌生,所以我可能会遗漏一些简单的东西......

【问题讨论】:

    标签: mongodb mongodump mongorestore


    【解决方案1】:

    事实证明,如果你有一个$regex,你也需要$options,即使$options = ''。错误消息暗示了这一点,但非常糟糕。从以下位置更改创建语句:

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

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

    解决问题

    【讨论】:

    • 这非常令人惊讶,因为没有空 options 的验证将被成功创建并且可以工作。 mongodump 也可以,只是看到 mongorestore 失败。不是很一致。验证创建的早期失败会更有意义。接受缺失的options:更好。 v4.0.3 中的行为仍然存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多