• MongoDB


    1.创建配置文件mongod.cfg,内容如下:
    systemLog:
        destination: file
        path: D:GreenSoftwareMongodbV3.6datalogmongod.log
    storage:
        dbPath: D:GreenSoftwareMongodbV3.6datadb

    2.安装 MongoDB服务

    D:GreenSoftwareMongodbV3.6inmongod.exe --config "D:GreenSoftwareMongodbV3.6mongod.cfg" --install

    D:GreenSoftwareMongodbV3.6inmongod.exemongod.exe --remove

    3.启动服务

    net start MongoDB

    net stop MongoDB


    4.在环境变量path中加入mongodb的bin目录

    D:GreenSoftwareMongodbV3.6in


    5.默认有4个库
    admin: 从权限的角度来看,这是"root"数据库。要是将一个用户添加到这个数据库,这个用户自动继承所有数据库的权限。一些特定的服务器端命令也只能从这个数据库运行,比如列出所有的数据库或者关闭服务器。
    local: 这个数据永远不会被复制,可以用来存储限于本地单台服务器的任意集合
    config: 当Mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息
    test:

    5.1通过后台管理SHELL连接到Monogo
    cd D:GreenSoftwareMongodbV3.6in
    mongo

    mongo -u root -p gis 127.0.0.1:27017/admin


    6.创建数据库(切换使用数据库)
    use geodb
    geodb.dropDatabase()

    7.列出数据库与显示当前使用的数据库
    show dbs

    db

    8.概念与DBMS对照

    数据库         -->      数据库
    集合            -->      表格
    文档            -->      行
    字段            -->      列
    嵌入文档     -->     表联合
    主键(MongoDB 提供了 key 为 _id )      -->      主键

    文档中的键/值对是有序的。
    MongoDB区分类型和大小写。
    MongoDB的文档不能有重复的键。
    文档的键是字符串。除了少数例外情况,键可以使用任意UTF-8字符。

    Capped collections 就是固定大小的collection。它有很高的性能以及队列过期的特性(过期按照插入的顺序). 有点和 "RRD" 概念类似

    元数据: dbname.system.*

    9.默认主键
    ObjectId: 包含 12 bytes:

            前 4 个字节表示创建 unix时间戳,

            接下来的 3 个字节是机器标识码,

             紧接的两个字节由进程 id 组成 PID,

             最后三个字节是随机数
    var newObject = ObjectId()
    newObject.getTimestamp()
    newObject.str


    10.创建集合
    db.createCollection("stations")
    db.stations.drop()

    11.列出集合
    show collections


    12.插入文档
    db.stations.insert({"stcd" : "43675896", "name" : "獅子山"})

    document=({
        stcd: '43675896',
        name: '马王坝',
        description: '此站建于2011所',
        crateTime: ISODate("2011-04-24 00:00:00"),
        type: ['PP', 'RR'],
        longitude: 108.43,
        latitude: 34.68,
        altitude: 132.24
    });

    db.stations.insert(document)

    document=({
        stcd: '43675896',
        name: '张峰沟',
        description: '此站建于2011所',
        crateTime: ISODate("2011-04-26 00:00:00"),
        type: ['PP', 'RR'],
        longitude: 108.92,
        latitude: 35.21,
        altitude: 142.24
    });

    db.stations.insert(document)

      

    13.显示集合中的所在文档
    db.stations.find()
    db.stations.find().pretty()


    13.更新文档

    1)一般更新

    db.stations.update(
    {
        name: '獅子山'
    },
    {
        $set:
        {
            stcd: '43675432',
            name: '獅子山',
            description: '此站建于2012所',
            crateTime: ISODate("2012-06-04 00:00:00"),
            type: ['PP'],
            longitude: 108.56,
            latitude: 34.79,
            altitude: 112.45
        }
    }
    );

    db.stations.find().pretty()

    db.collection.updateOne()
    db.collection.updateMany()


    db.stations.save({
        "_id" : ObjectId("5adf43329bc001eb7ee4c92d"),
        stcd: '43675436',
        name: '獅子山New',
        description: '此站建于2012所',
        crateTime: ISODate("2012-06-04 00:00:00"),
        type: ['PP'],
        longitude: 108.56,
        latitude: 34.79,
        altitude: 112.45
    });


    db.stations.find().pretty()

     2)用已有字段更新其它字段

    db.flyInfoLineChina.find().forEach(
        function(item){
            db.flyInfoLineChina.update({"_id":item._id},{"$set": {"count": item.location.coordinates.length}},false,true)
        }
    )


    14.删除文档

    db.stations.remove({stcd: '43675432'})

    删除所有
    db.stations.remove({})
    db.stations.deleteMany({})
    db.stations.deleteOne( {stcd: '43675432'} )


    15.创建索引
    db.stations.ensureIndex({stcd:1})


    16.创建用户
    db.createUser(
    {
        user: 'zyx',
        pwd: 'gis',
        roles:[
            {role: 'readWrite', db:'geodb'}
        ]
    }
    );

    db.dropUser('zyx')


    17.加入权限控制

    1> 加入管理员用户

    use admin

    db.createUser(
    {
        user: "root",
        pwd: "gis",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    }
    );

    db.dropUser('root')

    2> 启动权限
    在mongodb配置文件mongod.cfg中加入如下内容:
    security:
        authorization: enabled

      

    3>重启服务
    启动Mongodb,重新登录

    use admin
    db.auth('root','gis')


    18.查询
    $gt / $gte      >       >=
    $lt / $lte        <       <=
    $eq / $net    ==     !=

    $in / $nin     in       not in
    $or

    1>

    db.stations.find({'altitude':{$lt: 136}})

    2> And

    db.stations.find(
    {
    'altitude':{$lt: 136},
    'latitude':{$gt: 34.7}
    })

    3> Or
    db.stations.find({
    $or: [
    {'altitude':{$lt: 132}},
    {'latitude':{$gt: 34.7}}
    ]
    })

    4> 正则表达式
    db.stations.find({name:/山/});
    db.stations.find({name:/^獅子/});

    5> NULL
    db.stations.find({name:null})
    db.stations.find({name:{$ne:null}})

    6> EXISTS
    db.stations.find( { name: { $exists: true } } )
    db.stations.find( { name: { $exists: false } } )

    mongoimport -h 127.0.0.1 --port 27017 -u zyx -p gis --db geodb --collection flyInfoChina --type csv --headerline --drop --file "f:/dataChina/flyInfo20180615.csv"


    mongoexport -h 127.0.0.1 --port 27017 -u zyx -p gis -d geodb -c flyInfoChina --type=csv -f FlyID,Time,Height,Speed,Direction,Lat,Lon,FType,FlyDataType --sort {"FlyID":1,"Time":1} -o flyInfoChinaProject.csv

    排序导出
    mongoexport -h 127.0.0.1 --port 27017 -u zyx -p gis -d geodb -c flyInfoChina --type=csv -f FlyID,Time,Height,Speed,Direction,Lat,Lon,FType,FlyDataType --sort {"FlyID":1,"Time":1} -o flyInfoChinaProject.csv

    删除字段
    db.flyInfoChina.update({},{$unset:{"HLat":"","HLon":"","Yaw":"","Pitch":"","Roll":"","MID":"","Sign":"","Ext1":"","Ext2":"","IsLocate":"","GPS_C":"","GSM_S":"","Power":""}},false,true)

    建索引
    db.flyInfoChina.createIndex({"FlyID":1,"Time":1})

    用一字段更新另一字段

     db.flyInfoLineChina.find().forEach(

      function(item){
        db.flyInfoLineChina.update({"_id": item._id}, {"$set": {"count": item.location.coordinates.length}}, false, true);
      }
    )

    字段统计

    db.flyInfoLineChina.aggregate( { $group :  { _id : null,  countAll : { $sum : "$count" } } });

    db.flyInfoChina.findOne()

    db.flyInfoChina.find({"Yaw":{$gt:0}})

  • 相关阅读:
    NSURLConnection的异步请求方式
    <iOS>关于Xcode上的Other linker flags
    使用十六进制色值表示UIColor
    kubernetes & docker
    01 docker的安装与基本使用
    08 数组
    07 grep 正则
    06 信号处理和expect
    05 函数
    04 流程控制
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/8945005.html
Copyright © 2020-2023  润新知