mongodb執行外掛js腳本實現根據指定屬性分組,提取相關屬性輸出到csv文件

編寫js腳本,實現根據設備廠商型號分組並提取對應廠商、型號。

var cursor=db.getCollection('LatestDeviceInfo').aggregate(
[
{
        "$group":{  
            "_id":{     
                "manufacturer":"$manufacturer",
                "model":"$model"
            }
        }
    },{
        "$project":{
            "_id":false,
            "manufacturer":"$_id.manufacturer",
            "model":"$_id.model"
            }
        }

],{ allowDiskUse: true });
while(cursor.hasNext()){
        var obj=cursor.next();
        print(obj.manufacturer+","+obj.model);
        }

複製js到腳本文件dump.js中。mongo命令執行外部js腳本輸出結果。

./mongo 127.0.0.1:27017/new_db dump.js> result.txt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章