MongoDB基本操作

基本操作

简单操作

mongo    #连接mongodb

>show dbs    #查看所有数据库
local 0.078125GB
test 0.203125GB

>use local    #切换到local
switched to db local

> show collections    #查看local的所有collection
startup_log

>db.startup_log.find()    #产看startup_log
{ "_id" : "jlan-pc-1466044795232", "hostname" : "jlan-pc", "startTime",...}

> db.createCollection('startup_log2')    #创建collection
{ "ok" : 1 }

>db.startup_log.remove()    #清空collection

数据导出

mongoexport -d local -c startup_log -o startup_log.json    #把startup_log导出为json文件,在终端执行

mongoexport -d local -c startup_log --csv -f _id,hostname,startTime -o startup_log.csv    #startup_log导出为csv文件,--csv表示导出为csv格式,-f表示字段名;

数据导入

mongoimport -d local -c startup_log2 --file startup_log.json    #把json文件导入collection,--file用于指定文件

mongoimport -d local -c startup_log2 --type csv --headerline --file startup_log.csv    #把csv文件导入collection,--headerline表示忽略第一行

高级操作

修改collection的字段类型
mongo可以通过find(...).forEach(function(x) {})语法来修改collection的field类型。
假设collection为hotels_info,field为dpcount:

db.hotels_info.find({dpcount:{$exists:true}}).forEach(function(x){
    x.dpcount=new NumberInt(x.dpcount);
    db.hotels_info.save(x);
})

查询操作

db.hotels_info.find({'dpcount':{'$gte':200}},{'id':1,'name':1,'_id':0})    #第一个{}中设定查询条件,第二个{}中设定需要显示的字段
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 安装 配置环境 mongodb安装完毕后,默认安装路径在/usr/local/Cellar/mongodb/3.4...
    bd4d0d78f248阅读 11,057评论 0 53
  • 系统相关 安装MongoDB 启动MongoDB服务器 连接MongoDB服务器,启动客户端 数据库相关 创建数据...
    DongGuangqing阅读 1,805评论 0 0
  • Get Started Start mongo server: download mongodb create t...
    一颗北上广的心阅读 4,492评论 0 51
  • 原文链接 安装/卸载MongoDB 查看MongoDB版本信息 开启/关闭MongoDB服务 查看MongoDB是...
    sssnowyue阅读 1,738评论 0 0
  • mongodb需要先启动一个服务器进程(mongod命令),然后再启动客户端进程(mongo命令)。安装完成之后如...
    fooke阅读 3,680评论 0 0

友情链接更多精彩内容