MongoDB查詢操作

來源:互聯網
上載者:User

標籤:mail   利用   china   數學   地方   country   書籍   韓國   集合   

1 資料準備

var persons = [{
 name:"jim",
 age:25,
 email:"[email protected]",
 c:89,m:96,e:87,
 country:"USA",
 books:["JS","C++","EXTJS","MONGODB"]
},
{
 name:"tom",
 age:25,
 email:"[email protected]",
 c:75,m:66,e:97,
 country:"USA",
 books:["PHP","JAVA","EXTJS","C++"]
},
{
 name:"lili",
 age:26,
 email:"[email protected]",
 c:75,m:63,e:97,
 country:"USA",
 books:["JS","JAVA","C#","MONGODB"]
},
{
 name:"zhangsan",
 age:27,
 email:"[email protected]",
 c:89,m:86,e:67,
 country:"China",
 books:["JS","JAVA","EXTJS","MONGODB"]
},
{
 name:"lisi",
 age:26,
 email:"[email protected]",
 c:53,m:96,e:83,
 country:"China",
 books:["JS","C#","PHP","MONGODB"]
},
{
 name:"wangwu",
 age:27,
 email:"[email protected]",
 c:45,m:65,e:99,
 country:"China",
 books:["JS","JAVA","C++","MONGODB"]
},
{
 name:"zhaoliu",
 age:27,
 email:"[email protected]",
 c:99,m:96,e:97,
 country:"China",
 books:["JS","JAVA","EXTJS","PHP"]
},
{
 name:"piaoyingjun",
 age:26,
 email:"[email protected]",
 c:39,m:54,e:53,
 country:"Korea",
 books:["JS","C#","EXTJS","MONGODB"]
},
{
 name:"lizhenxian",
 age:27,
 email:"[email protected]",
 c:35,m:56,e:47,
 country:"Korea",
 books:["JS","JAVA","EXTJS","MONGODB"]
},
{
 name:"lixiaoli",
 age:21,
 email:"[email protected]",
 c:36,m:86,e:32,
 country:"Korea",
 books:["JS","JAVA","PHP","MONGODB"]
},
{
 name:"zhangsuying",
 age:22,
 email:"[email protected]",
 c:45,m:63,e:77,
 country:"Korea",
 books:["JS","JAVA","C#","MONGODB"]
}]
for(var i = 0;i<persons.length;i++){
 db.persons.insert(persons[i])
}
var persons = db.persons.find({name:"jim"})

2 查詢2.1 查詢出所有資料的指定鍵(name ,age ,country)

  db.persons.find({},{name:1,age:1,country:1,_id:0})

2.2 查詢條件

查詢出年齡在25到27歲之間的學生

   db.persons.find({age: {$gte:25,$lte:27},{_id:0,age:1})

查詢出所有不是韓國籍的學生的數學成績

   db.persons.find({country:{$ne:"Korea"}},{_id:0,m:1})

2.3 包含或不包含:$in或$nin

查詢國籍是中國或美國的學生資訊

   db.persons.find({country:{$in:["USA","China"]}})

查詢國籍不是中國或美國的學生資訊

   db.persons.find({country:{$nin:["USA","China"]}})

2.4 OR查詢:$or

查詢語文成績大於85或者英語大於90的學生資訊

   db.persons.find({$or:[{c:{$gte:85}},{e:{$gte:90}}]},{_id:0,c:1,e:1})

2.5 Null

把中國國籍的學生上增加新的鍵sex

    db.person.update({country:"China"},{$set:{sex:"m"}})

查詢出sex 等於 null的學生

    db.persons.find({sex:{$in:[null]}},{country:1})

2.6 正則查詢

查詢出名字中存在”li”的學生的資訊

    db.persons.find({name:/li/i},{_id:0,name:1})

2.7 $not的使用,$not可以用到任何地方進行取反操作

查詢出名字中不存在”li”的學生的資訊

     db.persons.find({name:{$not:/li/i}},{_id:0,name:1})

$not和$nin的區別是$not可以用在任何地方兒$nin是用到集合上的

2.8 數組查詢$all和index應用

查詢喜歡看MONGOD和JS的學生

     db.persons.find({books:{$all:["MONGOBD","JS"]}},{books:1,_id:0})

查詢第二本書是JAVA的學習資訊

     db.persons.find({"books.1":"JAVA"})

查詢指定長度數組$size它不能與比較查詢符一起使用(這是弊端)

查詢出喜歡的書籍數量是4本的學生

     db.persons.find({books:{$size:4}},{_id:0,books:1})

2.9 查詢出喜歡的書籍數量大於3本的學生

增加欄位size

     db.persons.update({},{$set:{size:4}},false, true)

改變書籍的更新方式,每次增加書籍的時候size增加1

     db.persons.update({查詢器},{$push:{books:"ORACLE"},$inc:{size:1}})

利用$gt查詢

     db.persons.find({size:{$gt:3}})

2.10 $slice操作符返迴文檔中指定數組的內部值

查詢出Jim書架中第2~4本書

     db.persons.find({name:"jim"},{books:{"$slice":[1,3]}})

查詢出最後一本書

     db.persons.find({name:"jim"},{books:{"$slice":-1},_id:0,name:1})

2.11 文檔查詢:$elemMatch

為jim添加學習簡曆文檔

var jim = [{
 school :"K",
 score:"A"
},{
 school :"L",
 score:"B"
},{
 school :"J",
 score:"A+"
}]
     db.persons.update({name:"jim"},{$set:{school:jim}})

查詢出在K上過學的學生

     db.persons.find({school:{$elemMatch:{school:"K",score:"A"}}})

2.12 Limit返回指定的資料條數

查詢出persons文檔中前5條資料

    db.persons.find({},{_id:0,name:1}).limit(5)

2.13 Skip返回指定資料的跨度

查詢出persons文檔中5~10條的資料

    db.persons.find({},{_id:0,name:1}).limit(5).skip(5)

2.14 .Sort返回按照年齡排序的資料[1,-1]

    db.persons.find({},{_id:0,name:1,age:1}).sort({age:1})

MongoDB查詢操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.