Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all _id field from subdocuments

I have been using Mongoose to insert a large amount of data into a mongodb database. I noticed that by default, Mongoose adds _id fields to all subdocuments, leaving me with documents which look like this (I've removed many fields for brevity - I've also shrunken each array to one entry, they generally have more)

{
    "start_time" : ISODate("2013-04-05T02:30:28Z"),
    "match_id" : 165816931,
    "players" : [
            {
                    "account_id" : 4294967295,
                    "_id" : ObjectId("51daffdaa78cee5c36e29fba"),
                    "additional_units" : [ ],
                    "ability_upgrades" : [
                            {
                                    "ability" : 5155,
                                    "time" : 141,
                                    "level" : 1,
                                    "_id" : ObjectId("51daffdaa78cee5c36e29fca")
                            },
                    ]
            },
    ],
     "_id" : ObjectId("51daffdca78cee5c36e2a02e")
}

I have found how to prevent Mongoose adding these by default (http://mongoosejs.com/docs/guide.html, see option: id), however I now have 95 million records with these extraneous _id fields on all subdocuments. I am interested in finding the best way of deleting all of these fields (leaving the _id on the top level document). My initial thoughts are to use a bunch of for...in loops on each object but this seems very inefficient.

like image 999
Charles A Avatar asked Oct 24 '25 02:10

Charles A


2 Answers

Given Derick's answer, I have created a function to do this:

var deleteIdFromSubdocs = function (obj, isRoot) {
for (var key in obj) {
    if (isRoot == false && key == "_id") {
        delete obj[key];
    } else if (typeof obj[key] == "object") {
        deleteIdFromSubdocs(obj[key], false);
    }
}
return obj;

And run it against a test collection using:

 db.testobjects.find().forEach(function (x){ y = deleteIdFromSubdocs(x, true); db.testobjects.save(y); } )

This appears to work for my test collection. I'd like to see if anyone has any opinions on how this could be done better/any risks involved before I run it against the 95 million document collection.

like image 88
Charles A Avatar answered Oct 26 '25 19:10

Charles A


The players._id could be removed using an update operation, like he following:

db.collection.update({'players._id': {$exists : 1}}, { $unset : { 'players.$._id' : 1 } }, false, true)

However, it's not possible use positional operator in nested arrays. So, one solution is run a script directly on our database:

var cursor = db.collection.find({'players.ability_upgrades._id': {$exists : 1}});

cursor.forEach(function(doc) {

    for (var i = 0; i < doc.players.length; i++) {
        var player = doc.players[i];
        delete player['_id'];

        for (var j = 0; j < player.ability_upgrades.length; j++) {
            delete player.ability_upgrades[j]['_id'];
        }
    }

    db.collection.save(doc);
});

Save the script to a file and call mongo with the file as parameter:

> mongo remove_oid.js --shell
like image 39
Miguel Cartagena Avatar answered Oct 26 '25 18:10

Miguel Cartagena