Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

44.10. 查询

44.10.1. find() MongoDB 2.x

查找所有 所有记录

			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'})
		

44.10.2. find() MongoDB 3.x

			db.getCollection('tracker').find({name:"81004892"})
		
Query
包含字段
		
db.getCollection('pyramidSelling').find({},{'phone':1})			
		
			
排除字段
				db.getCollection('pyramidSelling').find({},{'phone':0})
			
sort()

				db.getCollection('tracker').find({name:"81004892"}).sort({ctime: -1})
			

44.10.3. 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
	}
]