MongoDB脚本在集群中失败

时间:2019-03-03 12:32:52

标签: mongodb mongodb-query compose.io

我正在尝试对Compose.io中托管的MongoDB数据库进行数据库操作。我的mongodb脚本是

db.getCollection('companies').find({}).forEach(function(company){
    var oldHosts = company.hosts
    var newHosts = db.getCollection('hosts').find({'_id':{'$in':oldHosts},'original':true}).map(function(host){
        var newHostId = ObjectId()
        host._id = newHostId
        host.salt = ''
        host.hash = ''
    host.original = false
        host.company = company._id
        return host   
    })
    var hostEmails = newHosts.map(function(host){
        return host.email
    })
    var userEmailIDs = {}
    db.getCollection('users').find({'email':{'$in':hostEmails}}).forEach(function(user){
        userEmailIDs[user.email] = user._id
    })
    var insertHosts = newHosts.map(function(host){
        host.user = userEmailIDs[host.email]
        return host
    })
    db.getCollection('hosts').insert(insertHosts)
})

我在本地数据库中测试了此脚本,并且运行良好。但是在服务器中,我不断收到以下错误,

Error: getlasterror failed: {
    "singleShard" : "",
    "lastOp" : {
        "ts" : Timestamp(1551616205, 19),
        "t" : NumberLong(15)
    },
    "connectionId" : 156684,
    "codeName" : "WriteConcernFailed",
    "ok" : 0,
    "errmsg" : "election occurred after write",
    "code" : 64,
    "operationTime" : Timestamp(1551616208, 8),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1551616208, 8),
        "signature" : {
            "hash" : BinData(0,""),
            "keyId" : NumberLong("")
        }
    }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getLastErrorObj@src/mongo/shell/db.js:743:1
DBCollection.prototype._printExtraInfo@src/mongo/shell/collection.js:802:15
DBCollection.prototype.insert@src/mongo/shell/collection.js:387:5
@(shell):23:5
DBQuery.prototype.forEach@src/mongo/shell/query.js:501:1
@(shell):1:1

有人能找到原因吗?谢谢

0 个答案:

没有答案