WHCSRL 技术网

javascript 编程、操作、清洗 mongodb数据

1. 书写javascript脚本

连接数据库脚本,connect.js,内容:

function getConnection() {
    username = "root";
    password = "密码";
    this.getDB = function (dbname) {
        conn = new Mongo("localhost:27017");
        db = conn.getDB(dbname);
        db.auth(username,password);
        return db;
    }
}

 清洗数据脚本,fileStorageDeal.js,内容:

load("/mongoDataDealJs/js/base/connect.js");
conn = new getConnection();
db = conn.getDB("admin");
db = conn.getDB("数据库名称");
var cursor = db.getCollection('file_storage').find({});
while (cursor.hasNext()) {
    r = cursor.next();
    db.getCollection('file_record').insert({"username" : r.username,
        "status" : r.status,
        "createdDate" : r.createdDate,
        "lastModifiedDate" : r.lastModifiedDate,
        "_class" : r._class})
}

2. 进入mongodb安装目录bin;

3. 执行命令:mongo /mongoDataDealJs/js/fileStorageDeal/fileStorageDeal.js

推荐阅读