Tag: mongo

  • Mongodump reads from secondary by default in shard cluster with replica set

    When running mongodump against a mongos instance where the sharded cluster consists of replica sets, the read preference of the operation will prefer reads from secondary members of the set.

  • Mongo error 13431 have to have sort key in projection and removing it

    Error: When using shard collection and querying, if we explicitly want to omit a field but want to sort on that field, Mongo raises error code 13431 since mongos can’t sort without that missing field:

    db.collection.find({},{_id:0}).sort({_id:1});

    error: {
    “$err” : “have to have sort key in projection and removing it”,
    “code” : 13431
    }

    Solution: do not omit the field used in sort

  • Mongo cannot run command count(): failed on : [primary shard]

    Error: When using MongoDB sharded cluster 2.6 and php mongo driver,  db.collection.count() contains an $in operator would raise a BadValue error if using assoc array or bad numerical indexed array:

    array(‘apple’->’iphone’, ‘nokia’->’winphone’)

    array(0->’a’, ‘2’->b)

    Solution:

    In PHP, use array_values($array) to get the numerical indexed array as input for $in query

  • Mongo can’t canonicalize query: BadValue $in needs an array

    Error: From MongoDB 2.6, $in need an numerical indexed array. Associative array or malformed numerical indexed array like these will raise BadValue error:

    array(‘apple’->’iphone’, ‘nokia’->’winphone’)

    array(0->’a’, ‘2’->b)

    Solution:

    In PHP, use array_values($array) to get the numerical indexed array as input for $in query

  • Meteor: Uncaught Error: Not permitted. Untrusted code may only update documents by ID when update array element using $

    Error: Not permitted. Untrusted code may only update documents by ID when update sub array using $ in Meteor

    Cause:

    Suppose we have this doc:

    {

    _id: 1;

    tags: [

    {tag: “tag1”, count: 0},

    {tag: “tag2”, count: 0},

    ]

    }

    And we want to update tag1 count by inc with 1 with a client-side script:

    db.tags.update({_id:1, “tags.tag”: “tag1”}, {$inc: {“tags.$.count”: 1}})

    Even the above update only update a single document, Meteor will not allow us to do so because it expects only _id in the update condition.

    Solution: Update the whole tags with a modified value for tag1 count:

    db.tags.update({_id:1}, {$set: {“tags”: [{tag: “tag1”, count: 1},{tag: “tag2”, count: 0},]}});

  • Unable to activate mongo-1.12.0, because bson-2.3.0 conflicts with bson (= 1.12.0)

    Error: Unable to activate mongo-1.12.0, because bson-2.3.0 conflicts with bson (= 1.12.0) when installing both ruby gem mongo and mongoid

    Cause: mongo gem requires bson 1.12.0 while mongoid version > 3.0.0 requires moped which needs bson 2.3.0

    Solution: Use mongoid < 2.8.1 if you want to use mongo, or dump mongo and switch to moped:

    gem install mongoid -v 2.8.1

  • MongoDB Overflow sort stage buffered data usage exceeds internal limit

    Error: Overflow sort stage buffered data usage of 33558657 bytes exceeds internal limit of 33554432 bytes

    Causes: MongoDB has a 32MB limit on in-memory sort

    Solutions: Find out what field is used by the sort and create index or compound index for that fields. This will avoid the in-memory sort

    db.collection.ensureIndex({“a:1, “b”:1})

  • MongoDB MMS Point In Time restores are only available for the last 24 hours.

    Error: When restoring DB for replica set using MMS point in time, we have this error:

    “You are attempting to restore to a version no longer accessible for restore. Point In Time restores are only available for the last 24 hours.”

    Causes: Allow point-in-time restores going back (or oplog stored time) is set to more than 1 day but the snapshots interval is more than 12 hours (min 12 hours for Point in Time to work)

    Solutions: Allow point-in-time restores going back should be set to more than 1 days but the snapshot should be also captured at least every 12 hours

    Notes: Point in time only works for replica set. If you are using shard cluster, you should use checkpoint restore which allows to restore to any 15 mins point in time.

  • MongoDB MMS failed to decrypt password for job when restoring using SCP

    Causes:

    MMS Application Server and Backup Daemon are installed on different machines. And these two machines do not share the gen.key file.

    Solutions:

    On the MMS Application Server, run the following:

    grep ENC_KEY /opt/mongodb/mms/conf/mms.conf

    On the Backup Daemon Server:

    grep ENC_KEY /opt/mongodb/mms-backup-daemon/conf/daemon.conf

    If the gen.key file is missing on Backup Daemon Server or gen.key are different on the two machines, copy gen.key from MMS Application Server to Backup Daemon Server and restart Backup Daemon:

    sudo service mongodb-mms-backup-daemon restart

  • MongoDB MMS Backup failed to enqueue an oplog.

    Causes: Backup DB write error due to:

    1. run out of disk space

    2. no primary/master found

    Solution: Restart backup DB hosts should solve this error.