MongoDB 基本用法

[TOC]

MongoDB

导出

mongodump -h zx05 --port 11001 -d $db -c location20210719 --gzip -o ./dump
# 按条件导出
mongodump -h zx04 --port 1235 -d OneBms202104 -c bms20210405 -q '{"macid": "016880431463"}' --gzip -o ./dump016880431463.d

导入

mongorestore -h bmsdb:1235 --gzip --dir ./dump016880431463.d/
mongoimport -h zx01:5000 -d One -c test --file ./test.json

统计

db.alarm.aggregate([ {$match: {"alarmList.id": "02008001", "alarmList.fullBatteryId": ""} }, {$group: {_id: "$devId", total: {$sum: 1}}} ])

格式化字符串

db.location_15.find({macid: '016880627556'}, {battery_voltage: 1, ctime: 1, _id: 0}).sort({_id: -1}).limit(100).forEach(function (a) {
    function add0(m){
        return m<10?'0'+m:m 
    }

    function format(shijianchuo){
        var time = new Date(shijianchuo * 1000);
        var y = time.getFullYear();
        var m = time.getMonth()+1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
    }

    a["ctime"] = format(a["ctime"]); 
    printjson(a) 
})

索引

  • 创建索引
    db.bmsset.createIndex({macid: 1}, {background: true})
  • 查看索引
    db.bmsset.getIndexes()

发表评论