mongo的常用命令

107 阅读1分钟
1 登录命令
$/apps/product/mongodb/bin/mongo  127.0.0.1:27006/test  -u test -p test
MongoDB shell version: 3.0.3
connecting to:127.0.0.1:27006/test
Server has startup warnings:
2018-09-25T19:17:59.643+0800 I CONTROL  [initandlisten]
mongotest:PRIMARY>

2 help命令
mongotest:PRIMARY> help
	db.help()                    help on db methods
	db.mycoll.help()             help on collection methods
	sh.help()                    sharding helpers
	rs.help()                    replica set helpers
	help admin                   administrative help
	help connect                 connecting to a db help
	help keys                    key shortcuts
	help misc                    misc things to know
	help mr                      mapreduce

	show dbs                     show database names
	show collections             show collections in current database
	show users                   show users in current database
	show profile                 show most recent system.profile entries with time >= 1ms
	show logs                    show the accessible logger names
	show log [name]              prints out the last segment of log in memory, 'global' is default
	use <db_name>                set current database
	db.foo.find()                list objects in collection foo
	db.foo.find( { a : 1 } )     list objects in foo where a == 1
	it                           result of the last line evaluated; use to further iterate
	DBQuery.shellBatchSize = x   set default number of items to display on shell
	exit                         quit the mongo shell
3 查询相关命令

mongo中有数据库,但是数据库里不是表,而是集合,换了个名称。开始不知道,还一脸懵逼,看了help命令才知道。

show dbs                     show database names(显示所有数据库)
show collections             show collections in current database(显示当前数据下的集合(类似于mysql show tables;))
db.foo.find()                list objects in collection foo(查询当前数据库下foo集合中的所有记录,类似于 mysql中 select * from foo;)