假如我们在MongoDB服务器中有一个数据库,该数据中有一个数据集合(表)UserModel,存储的数据以及字段如下图所示:
- 查询集合(表)UserModel中的所有数据:
//select * from UserModel db.UserModel.find()
-
查询集合(表)UserModel中的所有数据,按照UserAge升序排列:
//select * from UserModel order by UserAge asc db.UserModel.find().sort({UserAge:1})//其中的1代表升序
-
查询集合(表)UserModel中的所有数据,按照UserAge降序排列:
//select * from UserModel order by UserAge desc db.UserModel.find().sort({UserAge:-1})//其中的-1代表降序
-
查询集合(表)UserModel中的前5条数据:
//select top 5 * from UserModel db.UserModel.find().limit(5)
-
查询集合(表)UserModel按照UserId升序查询前5条数据以后的所有数据:
//select * from UserModel where UserId not in(select top 5 UserId from UserModel order by UserId asc) order by UserId,按照UserId升序查询前5条数据以后的所有数据 db.UserModel.find().skip(5).sort({UserId:1})
-
查询集合(表)UserModel按照UserAge升序、第二页、每页10条的数据:
//分页查询,按照UserAge升序,查询第二页的数据,每页10条数据(下面语句中的2*10代表第二页*10) db.UserModel.find().limit(10).skip((2-1)*10).sort({UserAge:1})
-
查询集合(表)UserModel中UserId=6的数据:
//select * from UserModel where UserId=6 db.UserModel.find({UserId:6})
-
查询集合(表)UserModel中Status=1并且UserAge>29的数据:
//select * from UserModel where Status=1 and UserAge>29 db.UserModel.find({Status:1,UserAge:{$gt:29}})
-
查询集合(表)UserModel中UserName包含ac的数据:
//select * from UseModel where UserName like'%ac%' db.UserModel.find({UserName:/ac/})
-
查询集合(表)UserModel中UserName的前缀包含Qu的数据:
//select * from UserModel where UserName like'Qu%' db.UserModel.find({UserName:/^Qu/})
-
查询集合(表)UserModel中UserAge>13的数据:
//select * from UserModel where UserAge>13 db.UserModel.find({UserAge:{$gt:13}})
-
查询集合(表)UserModel中UserAge>=13的数据:
//select * from UserModel where UserAge>=13 db.UserModel.find({UserAge:{$gte:13}})
-
查询集合(表)UserModel中UserAge<13的数据:
//select * from UserModel where UserAge<13 db.UserModel.find({UserAge:{$lt:13}})
-
查询集合(表)UserModel中UserAge<=13的数据:
//select * from UserModel where UserAge<=13 db.UserModel.find({UserAge:{$lte:13}})
-
查询集合(表)UserModel中UserAge>=10并且UserAge<=20的数据:
//select * from UserModel where UserAge<=10 and UserAge<=20 db.UserModel.find({UserAge:{$gte:10,$lte:20}})
-
查询集合(表)UserModel中UserAge>45或UserAge<10的数据,并按照UserAge升序排列:
//select * from UserModel where UserAge>45 or UserAge<10 order by UserAge asc db.UserModel.find({$or:[{UserAge:{$gt:45}},{UserAge:{$lt:10}}]}).sort({UserAge:1})
-
查询集合(表)UserModel中的所有数据,并只查询UserId,UserName,UserAge这3个字段:
//select UserId,UserName,UserAge from UserModel db.UserModel.find({},{UserId:1,UserName:1,UserAge:1})
-
查询集合(表)UserModel中的所有数据,并只查询除UserId以外的所有字段:
//select UserName,UserAge,IsNormal,CreateTime from UserModel,查询出除UserId以外的所有列 db.UserModel.find({},{UserId:0})
-
查询集合(表)UserModel中UserAge>=10并且UserAge<=20的数据,并只查询出UserId,UserName,UserAge这3个字段:
//select UserId,UserName,UserAge from UserModel where UserAge<=10 and UserAge<=20 db.UserModel.find({UserAge:{$gte:10,$lte:20}},{UserId:1,UserName:1,UserAge:1})
-
查询集合(表)UserModel中的一条数据:
//select top 1 * from UserModel db.UserModel.findOne() 等价于 db.UserModel.find().limit(1)
-
查询集合(表)UserModel中满足查询条件的总条数:
//返回满足查询条件的集合总条数:select count(*) from UserModel where UserAge<=10 and UserAge<=20 db.UserModel.find({UserAge:{$gte:10,$lte:20}}).count()
-
查询集合(表)UserModel中满足查询条件的总条数(分页情况):
//如果是分页的查询,如果设置count(1)或count(true),则返回的条数就是当前页查询的结果总条数,而不是查询条件的总条数 db.UserModel.find({UserAge:{$gte:10,$lte:20}}).sort({UserAge:1}).limit(2).skip(5*2).count(1)
-
对集合(表)UserModel进行分组查询,按Status字段进行分组:
//分组查询,select Status,count(*) from UserModel group by Status db.UserModel.aggregate([ { $group: { _id:'$Status', 总数:{$sum:1} } } ])
-
对集合(表)UserModel中的Status字段去重查询:
//select distinct Status from UserModel db.UserModel.distinct('Status')
-
对集合(表)UserModel进行综合查询示例:
//综合查询示例语句(分页查询) /* 查询UserModel的第二页数据,每页5条数据 查询条件为:where Status=1 and (UserName like'%Qu%' or UserName like'Ja%') and (UserAge>=40 or UserAge<=20) 查询的字段为:UserId,UserName,UserAge,IsNormal,Dtl */ db.UserModel.find( { Status:1, $or:[{UserName:/Qu/},{UserName:/^Ja/}], $or:[{UserAge:{$gte:40}},{UserAge:{$lte:20}}] }, {UserId:1,UserName:1,UserAge:1,IsNormal:1,Dtl:1} ) .sort({UserAge:1}) .limit(5).skip((2-1)*5) //.count() //返回总条数
完整代码如下:
//select * from UserModel
db.UserModel.find()
//select * from UserModel order by UserAge asc
db.UserModel.find().sort({UserAge:1})
//select * from UserModel order by UserAge desc
db.UserModel.find().sort({UserAge:-1})
//select top 5 * from UserModel
db.UserModel.find().limit(5)
//select * from UserModel where UserId not in(select top 5 UserId from UserModel order by UserId asc) order by UserId,按照UserId升序查询前5条数据以后的所有数据
db.UserModel.find().skip(5).sort({UserId:1})
//分页查询,按照UserAge升序,查询第二页的数据,每页10条数据(下面语句中的2*10代表第二页*10)
db.UserModel.find().limit(10).skip((2-1)*10).sort({UserAge:1})
//select * from UserModel where UserId=6
db.UserModel.find({UserId:6})
//select * from UserModel where Status=1 and UserAge>29
db.UserModel.find({Status:1,UserAge:{$gt:29}})
//select * from UseModel where UserName like'%ac%'
db.UserModel.find({UserName:/ac/})
//select * from UserModel where UserName like'Qu%'
db.UserModel.find({UserName:/^Qu/})
//select * from UserModel where UserAge>13
db.UserModel.find({UserAge:{$gt:13}})
//select * from UserModel where UserAge>=13
db.UserModel.find({UserAge:{$gte:13}})
//select * from UserModel where UserAge<13
db.UserModel.find({UserAge:{$lt:13}})
//select * from UserModel where UserAge<=13
db.UserModel.find({UserAge:{$lte:13}})
//select * from UserModel where UserAge<=10 and UserAge<=20
db.UserModel.find({UserAge:{$gte:10,$lte:20}})
//select * from UserModel where UserAge>45 or UserAge<10 order by UserAge asc
db.UserModel.find({$or:[{UserAge:{$gt:45}},{UserAge:{$lt:10}}]}).sort({UserAge:1})
//select UserId,UserName,UserAge from UserModel
db.UserModel.find({},{UserId:1,UserName:1,UserAge:1})
//select UserName,UserAge,IsNormal,CreateTime from UserModel,查询出除UserId以外的所有列
db.UserModel.find({},{UserId:0})
//select UserId,UserName,UserAge from UserModel where UserAge<=10 and UserAge<=20
db.UserModel.find({UserAge:{$gte:10,$lte:20}},{UserId:1,UserName:1,UserAge:1})
//select top 1 * from UserModel
db.UserModel.findOne() 等价于 db.UserModel.find().limit(1)
//返回满足查询条件的集合总条数:select count(*) from UserModel where UserAge<=10 and UserAge<=20
db.UserModel.find({UserAge:{$gte:10,$lte:20}}).count()
//如果是分页的查询,如果设置count(1)或count(true),则返回的条数就是当前页查询的结果总条数,而不是查询条件的总条数
db.UserModel.find({UserAge:{$gte:10,$lte:20}}).sort({UserAge:1}).limit(2).skip(5*2).count(1)
//分组查询,select Status,count(*) from UserModel group by Status
db.UserModel.aggregate([
{
$group:
{
_id:'$Status',
总数:{$sum:1}
}
}
])
//select distinct Status from UserModel
db.UserModel.distinct('Status')
//综合查询示例语句(分页查询)
/*
查询UserModel的第二页数据,每页5条数据
查询条件为:where Status=1 and (UserName like'%Qu%' or UserName like'Ja%') and (UserAge>=40 or UserAge<=20)
查询的字段为:UserId,UserName,UserAge,IsNormal,Dtl
*/
db.UserModel.find(
{
Status:1,
$or:[{UserName:/Qu/},{UserName:/^Ja/}],
$or:[{UserAge:{$gte:40}},{UserAge:{$lte:20}}]
},
{UserId:1,UserName:1,UserAge:1,IsNormal:1,Dtl:1}
)
.sort({UserAge:1})
.limit(5).skip((2-1)*5)
//.count() //返回总条数