Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Email

第 4 章 命令工具

目录

4.1. mongo shell
4.1.1. mongo
4.1.2. help
4.1.3. show 查看命令
4.1.3.1. show dbs
4.1.3.2. show collections
4.1.3.3. show users
4.1.3.4. show profile
4.1.4. 切换数据库
4.1.5. 记录管理
4.1.5.1. save
4.1.5.2. insert
4.1.5.3. update
4.1.5.4. remove
4.1.5.5. find
4.1.5.6. count()
4.1.5.7. group()
4.1.6. 管道操作
4.1.7. shutdownServer
4.2. mongodump - Backup
4.3. mongorestore
4.4. mongostat
4.5. mongotop
4.6. mongofiles - Browse and modify a GridFS filesystem.
4.6.1. list 浏览文件
4.6.2. put 上传文件
4.6.3. get 下载
4.6.4. delete 删除

4.1. mongo shell

4.1.1. mongo

		
# mongo
MongoDB shell version: 2.2.3
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
>
		
		

3.4

		
[root@netkiller ~]# mongo
MongoDB shell version v3.4.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.1
Server has startup warnings: 
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] 
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] 
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] 
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] 
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-01-03T11:26:57.516+0800 I CONTROL  [initandlisten] 
>
		
		

4.1.2. help

help

db.help()                    help on DB methods
db.foo.help()                help on collection methods
		

4.1.3. show 查看命令

4.1.3.1. show dbs

show dbs show database names

			
> show dbs
local	(empty)
logging	0.203125GB
test	0.203125GB
			
			

4.1.3.2. show collections

show collections show collections in current database

			

> show collections
bios
system.indexes
			
			

另一种用法是show tables

			
> show tables
bios
system.indexes
			
			

4.1.3.3. show users

show users show users in current database

			

			
			

4.1.3.4. show profile

show profile show most recent system.profile entries with time >= 1ms

			
> show profile
db.system.profile is empty
Use db.setProfilingLevel(2) will enable profiling
Use db.system.profile.find() to show raw profile entries
			
			

4.1.4. 切换数据库

		
use <db name>                set curent database to <db name>

> use logging
switched to db logging
		
		

4.1.5. 记录管理

4.1.5.1. save

存储嵌套的对象

			
db.foo.save({'name':'neo','address':{'city':'shenzhen','post':518000},'phone':[13113668890,13322993040]})
			
			

存储数组对象

			
db.foo.save({'Uid':'netkiller@msn.com','phone':['13322993040','13113668890']})
			
			

4.1.5.2. insert

			
db.bios.insert(
   {
     _id: 1,
     name: { first: 'John', last: 'Backus' },
     birth: new Date('Dec 03, 1924'),
     death: new Date('Mar 17, 2007'),
     contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ],
     awards: [
               {
                 award: 'W.W. McDowell Award',
                 year: 1967,
                 by: 'IEEE Computer Society'
               },
               {
                 award: 'National Medal of Science',
                 year: 1975,
                 by: 'National Science Foundation'
               },
               {
                 award: 'Turing Award',
                 year: 1977,
                 by: 'ACM'
               },
               {
                 award: 'Draper Prize',
                 year: 1993,
                 by: 'National Academy of Engineering'
               }
             ]
   }
)
			
			

4.1.5.3. update

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

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

4.1.5.4. remove

删除uid=10的记录

			
db.foo.remove({'uid':10})
			
			

删除所有的记录

db.foo.remove()
			
4.1.5.4.1. 删除条件使用 _id
db.foo.remove({ "_id" : ObjectId("56e10b66a22ef1b1408b4567")})
				

4.1.5.5. find

查找所有 所有记录

db.foo.find()                list objects in collection foo
db.foo.find( { a : 1 } )     list objects in foo where a == 1
			

查找一条记录

db.foo.findOne()
			

根据条件检索10条记录

db.foo.find({'name':'neo'}).limit(10)
			

sort排序

db.foo.find({'name':'neo'}).sort({'Dt',-1})
db.foo.find().sort({'Ct':-1}).limit(1)
			

count记录统计操作

db.foo.count()
			

distinct操作,去重复查询指定列,

db.foo.distinct('name')
			

”>=”操作

			
db.foo.find({"timestamp": {"$gte" : 2}})
			
			

子对象的查找

db.foo.find({'address.city':'shenzhen'})
			

4.1.5.6. count()

			
> db.access.count()
51528
> db.access.count()
104401
			
			

4.1.5.7. group()

group()类似SQL中的Group by

			
> db.test.group({key: {remote_addr: true}, initial: {count: 0}, reduce: function(obj, prev) {prev.count++}});
[
	{
		"remote_addr" : "192.168.2.76",
		"count" : 3
	},
	{
		"remote_addr" : "192.168.2.70",
		"count" : 1
	}
]
			
			

4.1.6. 管道操作

		
cat data.bson | mongo test
		
		

4.1.7. shutdownServer

关闭MongoDB数据库

		
db.shutdownServer()
		
		
comments powered by Disqus