In MongoDB mapreduce, how can I flatten the values object?

前端 未结 7 2191
挽巷
挽巷 2020-12-04 22:22

I\'m trying to use MongoDB to analyse Apache log files. I\'ve created a receipts collection from the Apache access logs. Here\'s an abridged summary of what my

7条回答
  •  没有蜡笔的小新
    2020-12-04 22:30

    All the proposed solutions are far from optimal. The fastest you can do so far is something like:

    var flattenMRCollection=function(dbName,collectionName) {
        var collection=db.getSiblingDB(dbName)[collectionName];
    
        var i=0;
        var bulk=collection.initializeUnorderedBulkOp();
        collection.find({ value: { $exists: true } }).addOption(16).forEach(function(result) {
            print((++i));
            //collection.update({_id: result._id},result.value);
    
            bulk.find({_id: result._id}).replaceOne(result.value);
    
            if(i%1000==0)
            {
                print("Executing bulk...");
                bulk.execute();
                bulk=collection.initializeUnorderedBulkOp();
            }
        });
        bulk.execute();
    };
    

    Then call it: flattenMRCollection("MyDB","MyMRCollection")

    This is WAY faster than doing sequential updates.

提交回复
热议问题