mongo mysql 数据类型_征服 Mongodb 之 常用命令、基本数据类型

手里有本《MongoDB权威指南》,打算做个读书笔记,把常规命令、数据类型过一遍,强迫记忆。

50bb552c53801f5e0190347ee7c11d0c.png

集群配置相关链接:

基本操作相关链接:

关于如何安装、配置、启动MongoDB等,参考上篇文章。

一、常规命令

登录

类似于MySQL登录,可参考如下命令:

# mongo --help

MongoDB shell version: 2.0.7

usage: mongo [options] [db address] [file names (ending in .js)]

db address can be:

foo foo database on local machine

192.169.0.5/foo foo database on 192.168.0.5 machine

192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999

options:

--shell run the shell after executing files

--nodb don't connect to mongod on startup - no 'db address' arg expected

--norc will not run the ".mongorc.js" file on start up

--quiet be less chatty

--port arg port to connect to

--host arg server to connect to

--eval arg evaluate javascript

-u [ --username ] arg username for authentication

-p [ --password ] arg password for authentication

-h [ --help ] show this usage information

--version show version information

--verbose increase verbosity

--ipv6 enable IPv6 support (disabled by default)

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified

譬如,我经常这么用,一步到位:

# mongo 10.11.20.140/zlex

MongoDB shell version: 2.0.7

connecting to: 10.11.20.140/zlex

>

查看当前数据库/切换数据库

# mongo

MongoDB shell version: 2.0.7

connecting to: test

> db

test

> use zlex

switched to db zlex

>

查看当前数据库下的表,及索引

> show collections

system.indexes

test

user

system.indexes索引信息

test、user是数据表

查看当前数据库、表状态

> db.stats()

{

"db" : "test",

"collections" : 6,

"objects" : 16,

"avgObjSize" : 44.75,

"dataSize" : 716,

"storageSize" : 32768,

"numExtents" : 6,

"indexes" : 4,

"indexSize" : 32704,

"fileSize" : 201326592,

"nsSizeMB" : 16,

"ok" : 1

}

> db.blog.stats()

{

"ns" : "test.blog",

"count" : 0,

"size" : 0,

"storageSize" : 8192,

"numExtents" : 1,

"nindexes" : 1,

"lastExtentSize" : 8192,

"paddingFactor" : 1.56,

"flags" : 1,

"totalIndexSize" : 8176,

"indexSizes" : {

"_id_" : 8176

},

"ok" : 1

}

PS:MongoDB 1.8支持16MB的消息长度

一些边边角角的函数,你懂滴,横向思维下

10e12d57ce6042798b6d9bdae3515ec4.gif

> db.help()

DB methods:

db.addUser(username, password[, readOnly=false])

db.auth(username, password)

db.cloneDatabase(fromhost)

db.commandHelp(name) returns the help for the command

db.copyDatabase(fromdb, todb, fromhost)

db.createCollection(name, { size : ..., capped : ..., max : ... } )

db.currentOp() displays the current operation in the db

db.dropDatabase()

db.eval(func, args) run code server-side

db.getCollection(cname) same as db['cname'] or db.cname

db.getCollectionNames()

db.getLastError() - just returns the err msg string

db.getLastErrorObj() - return full status object

db.getMongo() get the server connection object

db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair

db.getName()

db.getPrevError()

db.getProfilingLevel() - deprecated

db.getProfilingStatus() - returns if profiling is on and slow threshold

db.getReplicationInfo()

db.getSiblingDB(name) get the db at the same server as this one

db.isMaster() check replica primary status

db.killOp(opid) kills the current operation in the db

db.listCommands() lists all the db commands

db.logout()

db.printCollectionStats()

db.printReplicationInfo()

db.printSlaveReplicationInfo()

db.printShardingStatus()

db.removeUser(username)

db.repairDatabase()

db.resetError()

db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }

db.serverStatus()

db.setProfilingLevel(level,) 0=off 1=slow 2=all

db.shutdownServer()

db.stats()

db.version() current version of the server

db.getMongo().setSlaveOk() allow queries on a replication slave server

db.fsyncLock() flush data to disk and lock server for backups

db.fsyncUnock() unlocks server following a db.fsyncLock()

>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值