基本操作
簡(jiǎn)單操作
mongo #連接mongodb
>show dbs #查看所有數(shù)據(jù)庫(kù)
local 0.078125GB
test 0.203125GB
>use local #切換到local
switched to db local
> show collections #查看local的所有collection
startup_log
>db.startup_log.find() #產(chǎn)看startup_log
{ "_id" : "jlan-pc-1466044795232", "hostname" : "jlan-pc", "startTime",...}
> db.createCollection('startup_log2') #創(chuàng)建collection
{ "ok" : 1 }
>db.startup_log.remove() #清空collection
數(shù)據(jù)導(dǎo)出
mongoexport -d local -c startup_log -o startup_log.json #把startup_log導(dǎo)出為json文件埠居,在終端執(zhí)行
mongoexport -d local -c startup_log --csv -f _id,hostname,startTime -o startup_log.csv #startup_log導(dǎo)出為csv文件廓块,--csv表示導(dǎo)出為csv格式,-f表示字段名;
數(shù)據(jù)導(dǎo)入
mongoimport -d local -c startup_log2 --file startup_log.json #把json文件導(dǎo)入collection,--file用于指定文件
mongoimport -d local -c startup_log2 --type csv --headerline --file startup_log.csv #把csv文件導(dǎo)入collection,--headerline表示忽略第一行
高級(jí)操作
修改collection的字段類型
mongo可以通過(guò)find(...).forEach(function(x) {})語(yǔ)法來(lái)修改collection的field類型幅慌。
假設(shè)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}) #第一個(gè){}中設(shè)定查詢條件般码,第二個(gè){}中設(shè)定需要顯示的字段