您的位置:首页 > 数据库 > Mongodb

MongoDb基本操作

2016-06-23 15:26 621 查看

基本操作

简单操作

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})    #第一个{}中设定查询条件,第二个{}中设定需要显示的字段
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mongodb 数据库