MongoDb命令查询所有数据库和表

MongoDb命令查询所有数据库和表

2023年7月26日发(作者:)

MongoDb命令查询所有数据库和表MongoDb 命令查询所有数据库列表

CODE:

> show dbs

如果想查看当前连接在哪个数据库下⾯,可以直接输⼊db

CODE:

> db

Admin

想切换到test数据库下⾯

CODE:

> use test

switched to db test

> db

Test

想查看test下有哪些表或者叫collection,可以输⼊

CODE:

> show collections

s

user

想知道mongodb⽀持哪些命令,可以直接输⼊help

CODE:

> help

Dos代码 收藏代码

HELP

show dbs show database names

show collections show collections in current database

show users show users in current database

show profile show most recent e entries with time >= 1ms

use set curent database to

() help on DB methods

() help on collection methods

() list objects in collection foo

( { a : 1 } ) list objects in foo where a == 1

it result of the last line evaluated; use to further iterate

如果想知道当前数据库⽀持哪些⽅法:

CODE:

> ();

Java代码 收藏代码

DB methods:

r(username, password) 添加数据库授权⽤户

(username, password) 访问认证

atabase(fromhost) 克隆数据库

dHelp(name) returns the help for the command

tabase(fromdb, todb, fromhost) 复制数据库

Collection(name, { size : ..., capped : ..., max : ... } ) 创建表

tOp() displays the current operation in the db

tabase() 删除当前数据库

_r(func, args) run code server-side

lection(cname) same as db['cname'] or

lectionNames() 获取当前数据库的表名

tError() - just returns the err msg string

tErrorObj() - return full status object

go() get the server connection object

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

e()

vError()

filingLevel()

licationInfo()

terDB(name) get the db at the same server as this onew

() kills the current operation in the db

ollectionStats() 打印各表的状态信息

eplicationInfo() 打印主数据库的复制状态信息

laveReplicationInfo() 打印从数据库的复制状态信息

hardingStatus() 打印分⽚状态信息

User(username) 删除数据库⽤户

Database() 修复数据库

rror()

mand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 } filingLevel(level) 0=off 1=slow 2=all

wnServer()

n() current version of the server

如果想知道当前数据库下的表或者表collection⽀持哪些⽅法,可以使⽤⼀下命令如:

CODE:

> (); user为表名

Java代码 收藏代码

DBCollection help

() 统计表的⾏数

ze() 统计表数据的⼤⼩

ct( key ) - eg. ct( 'x' ) 按照给定的条件除重

() drop the collection 删除表

dex(name) 删除指定索引

dexes() 删除所有索引

Index(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups 增加索引

( [query] , [fields]) - first parameter is an optional query filter. second parameter is optional set of fields to return.

根据条件查找数据

-----------------------

通过条件查询: ( { x : 77 } , { name : 1 , x : 1 } )

-----------------------------

如果想知道当前数据库下的表或者表collection⽀持哪些⽅法,可以使⽤⼀下命令如:

CODE:

> (); user为表名

Java代码 收藏代码

DBCollection help

() 统计表的⾏数

ze() 统计表数据的⼤⼩

ct( key ) - eg. ct( 'x' ) 按照给定的条件除重

() drop the collection 删除表

dex(name) 删除指定索引

dexes() 删除所有索引

Index(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups 增加索引

( [query] , [fields]) - first parameter is an optional query filter. second parameter is optional set of fields to return.

根据条件查找数据

-----------------------

通过条件查询: ( { x : 77 } , { name : 1 , x : 1 } )

-----------------------------

instead of connecting to a mongod instance

-v [ --verbose ] be more verbose (include multiple times for more

-vvvvv)

-o [ --out ] arg (=dump) output directory

[falcon@ ~/mongodb/bin]$ ./mongodump -d test -o test/

connected to: 127.0.0.1

DATABASE: test to test/test

to test/test/

100000 objects

s to test/test/

1 objects

[falcon@ ~/mongodb/bin]$ ls

2 mongo mongodump mongofiles mongorestore mongosniff

dump mongod mongoexport mongoimport mongos test

MongoDB的数据恢复⼯具mongorestore

查看test库中的表

CODE:

> show collections

s

User

删除user表

CODE:

> ();

True

> show collections

s

现在利⽤mongorestore表恢复刚才利⽤mongodump备份的数据

CODE:

[falcon@ ~/mongodb/bin]$ ./mongorestore --help

usage: ./mongorestore [options] [directory or filename to restore from]

options:

--help produce help message -h [ --host ] arg mongo host to connect to

-d [ --db ] arg database to use

-c [ --collection ] arg collection to use (some commands)

-u [ --username ] arg username

-p [ --password ] arg password

--dbpath arg directly access mongod data files in this path,

instead of connecting to a mongod instance

-v [ --verbose ] be more verbose (include multiple times for more

-vvvvv)

[falcon@ ~/mongodb/bin]$ ./mongorestore -d test -c user test/test/

connected to: 127.0.0.1

test/test/

going into namespace []

100000 objects

User表中的10w条记录已经恢复

CODE:

> show collections

s

user

> ();

{ "_id" : ObjectId("4b9c8db08ead0e3347000000"), "uid" : 1, "username" : "Falcon.C-1" }

{ "_id" : ObjectId("4b9c8db08ead0e3347010000"), "uid" : 2, "username" : "Falcon.C-2" }

{ "_id" : ObjectId("4b9c8db08ead0e3347020000"), "uid" : 3, "username" : "Falcon.C-3" }

{ "_id" : ObjectId("4b9c8db08ead0e3347030000"), "uid" : 4, "username" : "Falcon.C-4" }

{ "_id" : ObjectId("4b9c8db08ead0e3347040000"), "uid" : 5, "username" : "Falcon.C-5" }

.................

has more

1. 超级⽤户相关:

#增加或修改⽤户密码

r('admin','pwd')

#查看⽤户列表

()

#⽤户认证

('admin','pwd')

#删除⽤户

User('mongodb')

#查看所有⽤户

show users

#查看所有数据库

show dbs

#查看所有的collection

show collections

#查看各collection的状态

ollectionStats()

#查看主从复制状态

eplicationInfo()

#修复数据库

Database()

#设置记录profiling,0=off 1=slow 2=all

filingLevel(1)

#查看profiling

show profile

#拷贝数据库

tabase('mail_addr','mail_addr_tmp')

#删除collection

_()

#删除当前的数据库

tabase()

2. 客户端连接

/usr/local/mongodb/bin/mongo user_addr -u user -p 'pwd'

3. 增删改

#存储嵌套的对象

({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})

#存储数组对象

_({'Uid':'yushunzhi@','Al':['test-1@','test-2@']})

#根据query条件修改,如果不存在则插⼊,允许修改多条记录

({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)

#删除yy=5的记录

({'yy':5})

#删除所有的记录

()

4. 索引

增加索引:1(ascending),-1(descending)

Index({firstname: 1, lastname: 1}, {unique: true});

#索引⼦对象

_Index({'': 1})

#查看索引信息

r_exes()

r_exKeys()

#根据索引名删除索引

_dex('_1')

5. 查询

查找所有

()

#查找⼀条记录

e()

#根据条件检索10条记录

({'msg':'Hello 1'}).limit(10)

#sort排序

r_({'From':'yushunzhi@'}).sort({'Dt',-1})

r_().sort({'Ct':-1}).limit(1)

#count操作

_()

#distinct操作

ct('msg')

#>操作

({"timestamp": {"$gte" : 2}})

#⼦对象的查找

({'':'beijing'})

6. 管理

查看collection数据的⼤⼩

r_ze()

#查看colleciont状态

r_()

#查询所有索引的⼤⼩

r_ndexSize()

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690363329a338175.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信