资讯专栏INFORMATION COLUMN

MongoDB ( 四 )高级_find修饰符

crossea / 3172人阅读

摘要:返回内容查询出来后显示的结果样式,可以用和控制是否显示。修饰符文件使用进行链接对应的集合声明变量,并把查询结果赋值给利用游标的进行循环输出结果。到此的基础操作就结束了,但是我们还没有结束呢,下一节我们会用配合使用哦

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
odemongodb1> 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使用哦~~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19216.html

相关文章

  • MongoDB 学习笔记

    摘要:本文内容主要来自的学习,学习笔记基于个人理解对原书部分内容进行调整。如果需要练习相关命令行工具可直接阅读本学习笔记。笔者测试数据库版本较早,但文中涉及的所有概念及命令行工具基本适用于所有版本。二准备安装和运行服务在学习之前,需要安装环境。 感谢 Karl Seguin 编写的 The Little MongoDB Book 这本 MongoDB 入门书。 本文内容主要来自「The Li...

    makeFoxPlay 评论0 收藏0
  • MongoDB 学习笔记

    摘要:本文内容主要来自的学习,学习笔记基于个人理解对原书部分内容进行调整。如果需要练习相关命令行工具可直接阅读本学习笔记。笔者测试数据库版本较早,但文中涉及的所有概念及命令行工具基本适用于所有版本。二准备安装和运行服务在学习之前,需要安装环境。 感谢 Karl Seguin 编写的 The Little MongoDB Book 这本 MongoDB 入门书。 本文内容主要来自「The Li...

    刘永祥 评论0 收藏0
  • 以太坊开发实战学习-高级Solidity理论 (五)

    摘要:接上篇文章,这里继续学习高级理论。实战演练我们来写一个返回某玩家的整个僵尸军团的函数。但这样每做一笔交易,都会改变僵尸军团的秩序。在这里开始五可支付截至目前,我们只接触到很少的函数修饰符。 接上篇文章,这里继续学习Solidity高级理论。 一、深入函数修饰符 接下来,我们将添加一些辅助方法。我们为您创建了一个名为 zombiehelper.sol 的新文件,并且将 zombiefee...

    sushi 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<