Home | Mirror | Search

5. mongo 命令行

5.1. help

help

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

5.2. show 查看命令

show

		
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
use <db name>                set curent database to <db name>
		
		

5.3. 索引管理

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

增加索引

db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
		

索引子對象

		
db.logging.users.ensureIndex({address.city:1})
		
		

查看索引信息

db.logging.getIndexes()
[
	{
		"v" : 1,
		"key" : {
			"_id" : 1
		},
		"ns" : "logging.logging",
		"name" : "_id_"
	}
]
		

db.logging.users.getIndexKeys()

[ { "_id" : 1 }, { "name" : 1 } ]
		

根據索引名刪除索引

		
> db.logging.users.dropIndex('name_1')
{ "nIndexesWas" : 2, "ok" : 1 }

> db.logging.users.getIndexKeys()
[ { "_id" : 1 } ]
		
		

5.4. 記錄管理

5.4.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']})
			
			

5.4.2. update

根據query條件修改,如果不存在則插入,允許修改多條記錄

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

5.4.3. remove

刪除uid=10的記錄

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

刪除所有的記錄

db.foo.remove()
			

5.4.4. 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'})
			
comments powered by Disqus