【Mongodb】数据的高级查询

db.集合名称.find()

db.集合名称.findOne({})

db.集合名称.find({})

db.集合名称.find({}).pretty()

> db.test1.find({age:13})
{ "_id" : ObjectId("5f01dfadb17ac23a4b796ba5"), "name" : "zhang1", "addr" : "beijing", "age" : 13 }
{ "_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"), "name" : "zhang2", "addr" : "beijing", "age" : 13 }
{ "_id" : ObjectId("5f01dfbfb17ac23a4b796ba7"), "name" : "zhang2", "addr" : "shanghai", "age" : 13 }
{ "_id" : ObjectId("5f01dfc5b17ac23a4b796ba8"), "name" : "zhang3", "addr" : "shanghai", "age" : 13 }
> db.test1.find()
{ "_id" : ObjectId("5f01d623b17ac23a4b796b9f"), "name" : "zhangsan" }
{ "_id" : 11111, "name" : "lisi" }
{ "_id" : 11110, "name" : "lisi", "age" : "13" }
{ "_id" : ObjectId("5f01ddb5b17ac23a4b796ba0"), "name" : "wanger", "age" : 12 }
{ "_id" : ObjectId("5f01dfa5b17ac23a4b796ba4"), "name" : "zhang1", "addr" : "beijing", "age" : 12 }
{ "_id" : ObjectId("5f01dfadb17ac23a4b796ba5"), "name" : "zhang1", "addr" : "beijing", "age" : 13 }
{ "_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"), "name" : "zhang2", "addr" : "beijing", "age" : 13 }
{ "_id" : ObjectId("5f01dfbfb17ac23a4b796ba7"), "name" : "zhang2", "addr" : "shanghai", "age" : 13 }
{ "_id" : ObjectId("5f01dfc5b17ac23a4b796ba8"), "name" : "zhang3", "addr" : "shanghai", "age" : 13 }
> db.test1.find({"name":zhang2})
2020-07-05T22:13:47.779+0800 ReferenceError: zhang2 is not defined
> db.test1.find({"name":"zhang2"})
{ "_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"), "name" : "zhang2", "addr" : "beijing", "age" : 13 }
{ "_id" : ObjectId("5f01dfbfb17ac23a4b796ba7"), "name" : "zhang2", "addr" : "shanghai", "age" : 13 }
> db.test1.find({"name":"zhang2"}).pretty()
{
	"_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"),
	"name" : "zhang2",
	"addr" : "beijing",
	"age" : 13
}
{
	"_id" : ObjectId("5f01dfbfb17ac23a4b796ba7"),
	"name" : "zhang2",
	"addr" : "shanghai",
	"age" : 13
}
> db.test1.findOne({"name":"zhang2"})
{
	"_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"),
	"name" : "zhang2",
	"addr" : "beijing",
	"age" : 13
}

比较运算符

等于:默认是等于判断,没有运算符号

小于:$lt

小于等于:$lte

大于:$gt

大于等于:$gte

不等于:$ne

db.stru.find({age:{$gte:}})

 

范围运算符

使用"$in","$nin" 判断是否在某个范围内

查询年龄为18,28的学生

db.stu.find({age:{$in:[18,28,38]}})

> db.test1.find({name:"zhang2",age:12}})
2020-07-05T22:26:11.858+0800 SyntaxError: Unexpected token }
> db.test1.find({name:"zhang2",age:13}})
2020-07-05T22:26:19.498+0800 SyntaxError: Unexpected token }
> db.test1.find({name:"zhang2",age:13})
{ "_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"), "name" : "zhang2", "addr" : "beijing", "age" : 13 }
{ "_id" : ObjectId("5f01dfbfb17ac23a4b796ba7"), "name" : "zhang2", "addr" : "shanghai", "age" : 13 }
> db.test1.find({name:"zhang2",addr:{$in:["beijing"]}})
{ "_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"), "name" : "zhang2", "addr" : "beijing", "age" : 13 }

逻辑运算符

and:在语句中写多个条件即可

or:使用$or

查询年龄大于18,或者性别为false的学生

db.stu.find({$or:[{age:{$gt:18}},{gender:false}]})

查询年龄大于18或性别为男的学生,并且姓名是zhangsan

db.stu.find({$or:[{age:{$gt:18}},{gender:true}],name:"zhangsan"})

 

支持正则表达式

使用//或$regex编写正则表达式

db.products.find({sku:/^abc/})  查询字段sku 以abc结尾的

db.products.find({sku:{$regex:'789$'}})  查询字段sku以789结尾的

> db.test2.find({sku:/^abc/})
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description" }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
{ "_id" : 104, "sku" : "abc123", "description" : "Single line description" }
> db.test2.find({sku:{$regex:'789$'}})
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }

limit与skip

方法limit()  用于读取指定数量的数据

db.集合名称.find().limit(number)

方法skip()用于跳过指定数量的数据

db.集合名称.find().skip(number)

> db.test2.find().limit(2)
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description" }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
> db.test2.find().skip(2)
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before   line" }
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }
{ "_id" : 104, "sku" : "abc123", "description" : "Single line description" }

 

自定义查询

使用$where后面写一个函数,返回满足条件的数据,查询年龄大于30的学生

db.stu.find({$where:function(){

                    return this.age>30;

                 }})

> db.test1.find({$where:function(){return this.age=13}})
{ "_id" : ObjectId("5f01d623b17ac23a4b796b9f"), "name" : "zhangsan" }
{ "_id" : 11111, "name" : "lisi" }
{ "_id" : 11110, "name" : "lisi", "age" : "13" }
{ "_id" : ObjectId("5f01ddb5b17ac23a4b796ba0"), "name" : "wanger", "age" : 12 }
{ "_id" : ObjectId("5f01dfa5b17ac23a4b796ba4"), "name" : "zhang1", "addr" : "beijing", "age" : 12 }
{ "_id" : ObjectId("5f01dfadb17ac23a4b796ba5"), "name" : "zhang1", "addr" : "beijing", "age" : 13 }
{ "_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"), "name" : "zhang2", "addr" : "beijing", "age" : 13 }
{ "_id" : ObjectId("5f01dfbfb17ac23a4b796ba7"), "name" : "zhang2", "addr" : "shanghai", "age" : 13 }
{ "_id" : ObjectId("5f01dfc5b17ac23a4b796ba8"), "name" : "zhang3", "addr" : "shanghai", "age" : 13 

投影操作
在返回结果中,只选择需要显示的字段
字符1表示显示,0表示不显示
特殊 对于_id 默认是显示的,如果不显示需要明确设置为0
> db.test1.find({$where:function(){return this.age=13}},{name:1})
{ "_id" : ObjectId("5f01d623b17ac23a4b796b9f"), "name" : "zhangsan" }
{ "_id" : 11111, "name" : "lisi" }
{ "_id" : 11110, "name" : "lisi" }
{ "_id" : ObjectId("5f01ddb5b17ac23a4b796ba0"), "name" : "wanger" }
{ "_id" : ObjectId("5f01dfa5b17ac23a4b796ba4"), "name" : "zhang1" }
{ "_id" : ObjectId("5f01dfadb17ac23a4b796ba5"), "name" : "zhang1" }
{ "_id" : ObjectId("5f01dfb2b17ac23a4b796ba6"), "name" : "zhang2" }
{ "_id" : ObjectId("5f01dfbfb17ac23a4b796ba7"), "name" : "zhang2" }
{ "_id" : ObjectId("5f01dfc5b17ac23a4b796ba8"), "name" : "zhang3" }

 

排序操作

升序

db.stu.find().sort({age:1})

db.stu.find().sort({age:1,gender:-1})

降序

db.stu.find().sort({age:-1})

 

 

统计数目 .count()

消除重复

db.stu.distinct(‘hometown’,{age:{$gt:18}})

> db.test1.distinct("age")
[ "13", 12, 13 ]
> db.test1.count()
9

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值