0

I am trying to do a database operation for a MongoDB database hosted in Compose.io. My mongodb script is

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

I test this script in local database and it worked fine. But in the server I keep getting the following error,

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

Can anyoone find the what is the reason for this? Thanks

  • What is your replica set status(rs.status()) in production? – Mani Mar 3 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.