MongoDB ( 四 )高階_find修飾符

Meils發表於2018-04-01

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
odemongodb 1> 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 }
);

其他查詢修飾符

  • 不等修飾符
  1. 小於($lt):英文全稱less-than
  2. 小於等於($lte):英文全稱less-than-equal
  3. 大於($gt):英文全稱greater-than
  4. 大於等於($gte):英文全稱greater-than-equal
  5. 不等於($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引數:
  1. query:這個就是查詢條件,MongoDB預設的第一個引數。
  2. fields:(返回內容)查詢出來後顯示的結果樣式,可以用true和false控制是否顯示。
  3. limit:返回的數量,後邊跟數字,控制每次查詢返回的結果數量。
  4. skip:跳過多少個顯示,和limit結合可以實現分頁。
  5. 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使用哦~~

相關文章