MongoDB ( 四 )高级_find修饰符
find查询操作是我们平时再开发中最常用的,也是重中之重。
find基本操作符// 批量插入数据 var workmate1={ name:'JSPang', age:33, sex:1, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', skillThree:'PHP' }, regeditTime:new Date(), interest:[] } var workmate2={ name:'ShengLei', age:31, sex:1, job:'JAVA后端', skill:{ skillOne:'HTML+CSS', skillTwo:'J2EE', skillThree:'PPT' }, regeditTime:new Date(), interest:[] } var workmate3={ name:'MinJie', age:18, sex:0, job:'UI', skill:{ skillOne:'PhotoShop', skillTwo:'UI', skillThree:'PPT' }, regeditTime:new Date(), interest:[] } var workmate4={ name:'XiaoWang', age:25, sex:1, job:'UI', skill:{ skillOne:'PhotoShop', skillTwo:'UI', skillThree:'PPT' }, regeditTime:new Date(), interest:[] } var workmate5={ name:'LiangPeng', age:28, sex:1, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', }, regeditTime:new Date(), interest:[] } var workmate6={ name:'HouFei', age:25, sex:0, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', }, regeditTime:new Date(), interest:[] } var workmate7={ name:'LiuYan', age:35, sex:0, job:'美工', skill:{ skillOne:'PhotoShop', skillTwo:'CAD', }, regeditTime:new Date(), interest:[] } var workmate8={ name:'DingLu', age:20, sex:0, job:'美工', skill:{ skillOne:'PhotoShop', skillTwo:'CAD', }, regeditTime:new Date(), interest:[] } var workmate9={ name:'JiaPeng', age:29, sex:1, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', skillThree:'PHP' }, regeditTime:new Date(), interest:[] } var workmate10={ name:'LiJia', age:26, sex:0, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', skillThree:'PHP' }, regeditTime:new Date(), interest:[] } var db=connect('company'); var workmateArray=[workmate1,workmate2,workmate3,workmate4,workmate5,workmate6,workmate7,workmate8,workmate9,workmate10]; db.workmate.insert(workmateArray); print('[SUCCESS]:The data was inserted successfully');
// 启动数据库 PS D:\myweb\node\mongodb\01> mongo // 启动mongo MongoDB shell version v3.4.10 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.10 Server has startup warnings: 2018-04-01T17:32:00.547+0800 I CONTROL [initandlisten] 2018-04-01T17:32:00.548+0800 I CONTROL [initandlisten] ** WARNIN G: Access control is not enabled for the database. 2018-04-01T17:32:00.548+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricte d. 2018-04-01T17:32:00.548+0800 I CONTROL [initandlisten] > load('./demo.js') // 加载js文件批量插入 connecting to: mongodb://127.0.0.1:27017/company MongoDB server version: 3.4.10 [SUCCESS]:The data was inserted successfully true >
// 我们需要查找技能一中会HTML 和 CSS 的所有人 db.workmate.find({"skill.skillOne": "HTML+CSS"});
筛选字段
// 如我们只需要姓名和技能 db.workmate.find({"skill.skillOne": "HTML+CSS"}, { name: true, "skill.skillOne": true}); // find()的第二个参数用于指定要返回的字段 > db.workmate.find({"skill.skillOne": "HTML+CSS"}, { name: true, "skill.skillOne": true}); { "_id" : ObjectId("5ac0ba2d945d6de2ecdcd0ec"), "name" : "JSPang", "skill" : { "skillOne" : "HTML+CSS" } } { "_id" : ObjectId("5ac0ba2d945d6de2ecdcd0ed"), "name" : "ShengLei", "skill" : { "skillOne" : "HTML+CSS" } } { "_id" : ObjectId("5ac0ba2d945d6de2ecdcd0f0"), "name" : "LiangPeng", "skill" : { "skillOne" : "HTML+CSS" } } { "_id" : ObjectId("5ac0ba2d945d6de2ecdcd0f1"), "name" : "HouFei", "skill" : { "skillOne" : "HTML+CSS" } } { "_id" : ObjectId("5ac0ba2d945d6de2ecdcd0f4"), "name" : "JiaPeng", "skill" : { "skillOne" : "HTML+CSS" } } { "_id" : ObjectId("5ac0ba2d945d6de2ecdcd0f5"), "name" : "LiJia", "skill" : { "skillOne" : "HTML+CSS" } } > // 如果我们不想要_id db.workmate.find( {"skill.skillOne":"HTML+CSS"}, { name:true, "skill.skillOne":true, _id:false } );
其他查询修饰符
- 不等修饰符
- 小于($lt):英文全称less-than
- 小于等于($lte):英文全称less-than-equal
- 大于($gt):英文全称greater-than
- 大于等于($gte):英文全称greater-than-equal
- 不等于($ne):英文全称not-equal
db.workmate.find( {age:{$lte:30,$gte:25}}, {name:true,age:true,"skill.skillOne":true,_id:false} )
日期查找
var startDate= new Date('01/01/2018'); db.workmate.find( {regeditTime:{$gt:startDate}}, {name:true,age:true,"skill.skillOne":true,_id:false} )find多条件查询
$in
db.workmate.find({age:{$in:[25,33]}}, {name:1,"skill.skillOne":1,age:1,_id:0} ) // 查询年龄是 25 和 33 的
$or
db.workmate.find({$or:[ {age:{$gte:30}}, {"skill.skillThree":'PHP'} ]}, {name:1,"skill.skillThree":1,age:1,_id:0} )
$and
db.workmate.find({$and:[ {age:{$gte:30}}, {"skill.skillThree":'PHP'} ]}, {name:1,"skill.skillThree":1,age:1,_id:0} )
$not
db.workmate.find({ age:{ $not:{ $lte:30, $gte:20 } } }, {name:1,"skill.skillOne":1,age:1,_id:0} )
- 更多请查看下面的文档
Mongodb 重温之路(二)
find 数组查询// 数据中增加了一个数组 var workmate1={ name:'JSPang', age:33, sex:1, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', skillThree:'PHP' }, regeditTime:new Date(), interest:['看电影','看书','吃美食','钓鱼','旅游'] } var workmate2={ name:'ShengLei', age:31, sex:1, job:'JAVA后端', skill:{ skillOne:'HTML+CSS', skillTwo:'J2EE', skillThree:'PPT' }, regeditTime:new Date(), interest:['篮球','看电影','做饭'] } var workmate3={ name:'MinJie', age:18, sex:0, job:'UI', skill:{ skillOne:'PhotoShop', skillTwo:'UI', skillThree:'PPT' }, regeditTime:new Date(), interest:['做饭','画画','看电影'] } var workmate4={ name:'XiaoWang', age:25, sex:1, job:'UI', skill:{ skillOne:'PhotoShop', skillTwo:'UI', skillThree:'PPT' }, regeditTime:new Date(), interest:['写代码','篮球','画画'] } var workmate5={ name:'LiangPeng', age:28, sex:1, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', }, regeditTime:new Date(), interest:['玩游戏','写代码','做饭'] } var workmate6={ name:'HouFei', age:25, sex:0, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', }, regeditTime:new Date(), interest:['化妆','读书','做饭'] } var workmate7={ name:'LiuYan', age:35, sex:0, job:'美工', skill:{ skillOne:'PhotoShop', skillTwo:'CAD', }, regeditTime:new Date(), interest:['画画','聚会','看电影'] } var workmate8={ name:'DingLu', age:20, sex:0, job:'美工', skill:{ skillOne:'PhotoShop', skillTwo:'CAD', }, regeditTime:new Date(), interest:['美食','看电影','做饭'] } var workmate9={ name:'JiaPeng', age:29, sex:1, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', skillThree:'PHP' }, regeditTime:new Date(), interest:['写代码','篮球','游泳'] } var workmate10={ name:'LiJia', age:26, sex:0, job:'前端', skill:{ skillOne:'HTML+CSS', skillTwo:'JavaScript', skillThree:'PHP' }, regeditTime:new Date(), interest:['玩游戏','美食','篮球'] } var db=connect('company'); var workmateArray=[workmate1,workmate2,workmate3,workmate4,workmate5,workmate6,workmate7,workmate8,workmate9,workmate10]; db.workmate.insert(workmateArray); print('[SUCCESS]:The data was inserted successfully');
基本数组查询
// 比如现在我们知道了一个人的爱好是'画画','聚会','看电影',但我们不知道是谁,这时候我们就可以使用最简单的数组查询 > db.workmate.find({interest: ['画画','聚会','看电影']}, { ... name: true, interest: true, age: true, _id: 0 ... }); { "name" : "LiuYan", "age" : 35, "interest" : [ "画画", "聚会", "看电影" ] }
// 想看兴趣中含有看电影的员工 > db.workmate.find({interest: '看电影'}, {name: 1, _id: 0, interest: 1}) { "name" : "JSPang", "interest" : [ "看电影", "看书", "吃美食", "钓鱼", "旅游" ] } { "name" : "ShengLei", "interest" : [ "篮球", "看电影", "做饭" ] } { "name" : "MinJie", "interest" : [ "做饭", "画画", "看电影" ] } { "name" : "LiuYan", "interest" : [ "画画", "聚会", "看电影" ] } { "name" : "DingLu", "interest" : [ "美食", "看电影", "做饭" ] }
$all-数组多项查询
// 要查询出喜欢看电影和看书的人员信息, > db.workmate.find( ... {interest: {$all : ['看电影', '看书']}}, ... {name: 1, age: 1, _id: 0, interest: true} ... ); { "name" : "JSPang", "age" : 33, "interest" : [ "看电影", "看书", "吃美食", "钓鱼", "旅游" ] } >
数组的$in或者查询
// 看电影和看书有一样就行了 > db.workmate.find( ... {interest:{$in:["看电影","看书"]}}, ... {name:1,interest:1,age:1,_id:0} ... ) { "name" : "JSPang", "age" : 33, "interest" : [ "看电影", "看书", "吃美食", "钓鱼", "旅游" ] } { "name" : "ShengLei", "age" : 31, "interest" : [ "篮球", "看电影", "做饭" ] } { "name" : "MinJie", "age" : 18, "interest" : [ "做饭", "画画", "看电影" ] } { "name" : "LiuYan", "age" : 35, "interest" : [ "画画", "聚会", "看电影" ] } { "name" : "DingLu", "age" : 20, "interest" : [ "美食", "看电影", "做饭" ] } >
$size-数组个数查询
// 查询数组长度 db.workmate.find( {interest:{$size:5}}, {name:1,interest:1,age:1,_id:0} )
$slice-显示选项
// 比如我们现在想显示每个人兴趣的前两项,而不是把每个人所有的兴趣都显示出来。 > db.workmate.find( ... {}, ... {name:1,interest:{$slice:2},age:1,_id:0} ... ) { "name" : "JSPang", "age" : 33, "interest" : [ "看电影", "看书" ] } { "name" : "ShengLei", "age" : 31, "interest" : [ "篮球", "看电影" ] } { "name" : "MinJie", "age" : 18, "interest" : [ "做饭", "画画" ] } { "name" : "XiaoWang", "age" : 25, "interest" : [ "写代码", "篮球" ] } { "name" : "LiangPeng", "age" : 28, "interest" : [ "玩游戏", "写代码" ] } { "name" : "HouFei", "age" : 25, "interest" : [ "化妆", "读书" ] } { "name" : "LiuYan", "age" : 35, "interest" : [ "画画", "聚会" ] } { "name" : "DingLu", "age" : 20, "interest" : [ "美食", "看电影" ] } { "name" : "JiaPeng", "age" : 29, "interest" : [ "写代码", "篮球" ] } { "name" : "LiJia", "age" : 26, "interest" : [ "玩游戏", "美食" ] } > // 如果我们想显示兴趣的最后一项,可以直接使用slice:-1,来进行查询。 > db.workmate.find( ... {}, ... {name:1,interest:{$slice:-1},age:1,_id:0} ... ) { "name" : "JSPang", "age" : 33, "interest" : [ "旅游" ] } { "name" : "ShengLei", "age" : 31, "interest" : [ "做饭" ] } { "name" : "MinJie", "age" : 18, "interest" : [ "看电影" ] } { "name" : "XiaoWang", "age" : 25, "interest" : [ "画画" ] } { "name" : "LiangPeng", "age" : 28, "interest" : [ "做饭" ] } { "name" : "HouFei", "age" : 25, "interest" : [ "做饭" ] } { "name" : "LiuYan", "age" : 35, "interest" : [ "看电影" ] } { "name" : "DingLu", "age" : 20, "interest" : [ "做饭" ] } { "name" : "JiaPeng", "age" : 29, "interest" : [ "游泳" ] } { "name" : "LiJia", "age" : 26, "interest" : [ "篮球" ] } >find参数使用
find方法的第一个参数(query)和第二个参数(fields)
- find参数:
- query:这个就是查询条件,MongoDB默认的第一个参数。
- fields:(返回内容)查询出来后显示的结果样式,可以用true和false控制是否显示。
- limit:返回的数量,后边跟数字,控制每次查询返回的结果数量。
- skip:跳过多少个显示,和limit结合可以实现分页。
- sort:排序方式,从小到大排序使用1,从大到小排序使用-1。
$where修饰符
db.workmate.find( {$where:"this.age>30"}, {name:true,age:true,_id:false} )js文件使用find()
var db = connect("company") //进行链接对应的集合collections var result = db.workmate.find() //声明变量result,并把查询结果赋值给result //利用游标的hasNext()进行循环输出结果。 while(result.hasNext()){ printjson(result.next()) //用json格式打印结果 }
var db = connect("company") //进行链接对应的集合collections var result = db.workmate.find() //声明变量result,并把查询结果赋值给result //利用游标的hasNext()进行循环输出结果。 result.forEach(function(result){ printjson(result) })到此mongoDB的基础操作就结束了,但是我们还没有结束呢,下一节我们会用nodejs配合Mongodb使用哦~~
相关推荐
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19