編寫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