MongoDB之資料查詢(關係運算)
在MongoDB裡面支援的關係查詢操作:大於($gt),小於($lt),大於等於($gte),小於等於($lte),不等於($ne),等於(key:value,$eq)。
範例:定義一個員工資訊集合
db.emp.drop();
db.emp.insert({"name":"趙一","sex":"男","age":30,"sal":1000,"loc":"北京"});
db.emp.insert({"name":"錢二","sex":"女","age":22,"sal":5000,"loc":"上海"});
db.emp.insert({"name":"孫三","sex":"男","age":40,"sal":2000,"loc":"深圳"});
db.emp.insert({"name":"李四","sex":"女","age":30,"sal":7000,"loc":"北京"});
db.emp.insert({"name":"週五","sex":"女","age":30,"sal":6400,"loc":"北京"});
db.emp.insert({"name":"吳六","sex":"男","age":30,"sal":2500,"loc":"重慶"});
db.emp.insert({"name":"鄭七","sex":"女","age":50,"sal":4700,"loc":"成都"});
db.emp.insert({"name":"王八","sex":"男","age":35,"sal":8000,"loc":"北京"});
> db.emp.find().pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "週五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
範例:查詢姓名為孫三的資訊
> db.emp.find({"name":"孫三"}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
範例:查詢性別為男的人員資訊
> db.emp.find({"sex":"男"}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
範例:查詢年齡大於30的人員
> db.emp.find({"age":{"$gt":30}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
範例:查詢薪水大於等於4700的人員
> db.emp.find({"sal":{"$gte":4700}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "週五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
範例:查詢姓名不是孫三的資訊
> db.emp.find({"name":{"$ne":"孫三"}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "週五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
此時與之前最大的區別就在於,在一個JSON結構裡面需要定義其他的JSON結構,並且這種風格在進行程式操作的時候依然如此。
範例:定義一個員工資訊集合
db.emp.drop();
db.emp.insert({"name":"趙一","sex":"男","age":30,"sal":1000,"loc":"北京"});
db.emp.insert({"name":"錢二","sex":"女","age":22,"sal":5000,"loc":"上海"});
db.emp.insert({"name":"孫三","sex":"男","age":40,"sal":2000,"loc":"深圳"});
db.emp.insert({"name":"李四","sex":"女","age":30,"sal":7000,"loc":"北京"});
db.emp.insert({"name":"週五","sex":"女","age":30,"sal":6400,"loc":"北京"});
db.emp.insert({"name":"吳六","sex":"男","age":30,"sal":2500,"loc":"重慶"});
db.emp.insert({"name":"鄭七","sex":"女","age":50,"sal":4700,"loc":"成都"});
db.emp.insert({"name":"王八","sex":"男","age":35,"sal":8000,"loc":"北京"});
> db.emp.find().pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "週五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
範例:查詢姓名為孫三的資訊
> db.emp.find({"name":"孫三"}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
範例:查詢性別為男的人員資訊
> db.emp.find({"sex":"男"}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
範例:查詢年齡大於30的人員
> db.emp.find({"age":{"$gt":30}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
範例:查詢薪水大於等於4700的人員
> db.emp.find({"sal":{"$gte":4700}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "週五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
範例:查詢姓名不是孫三的資訊
> db.emp.find({"name":{"$ne":"孫三"}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "週五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
此時與之前最大的區別就在於,在一個JSON結構裡面需要定義其他的JSON結構,並且這種風格在進行程式操作的時候依然如此。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2144293/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MongoDB之資料查詢(邏輯運算)MongoDB
- MongoDB之資料查詢(範圍運算)MongoDB
- MongoDB之資料查詢操作MongoDB
- MongoDB之資料查詢(資料排序)MongoDB排序
- 資料庫 - 關係代數與關係運算資料庫
- MongoDB之資料查詢(陣列)MongoDB陣列
- MongoDB 怎麼計運算元查詢MongoDB
- MongoDB之資料查詢(巢狀集合)MongoDB巢狀
- MongoDB之資料查詢(分頁顯示)MongoDB
- MongoDB之資料查詢(正規表示式)MongoDB
- MongoDB之資料查詢(欄位判斷)MongoDB
- nodejs之MongoDB 非關係型資料庫NodeJSMongoDB資料庫
- MongoDB之資料查詢(where條件過濾)MongoDB
- MongoDB資料庫中查詢資料(下)MongoDB資料庫
- 資料庫AR之關聯查詢資料庫
- Mongodb 關聯表查詢MongoDB
- 在MongoDB資料庫中查詢資料(上)MongoDB資料庫
- Oracle 查詢鎖之間的依賴關係Oracle
- 關係型資料庫查詢語言 SQL 和圖資料庫查詢語言 nGQL 對比資料庫SQL
- sql運算元據庫(3)-->外來鍵約束、資料庫表之間的關係、三大正規化、多表查詢、事務SQL資料庫
- 聊聊非關係型資料庫MongoDB索引資料庫MongoDB索引
- ORACLE查詢表之間的主外來鍵關係Oracle
- Python全棧 MongoDB 資料庫(資料的查詢)Python全棧MongoDB資料庫
- Python全棧MongoDB資料庫(資料的查詢)Python全棧MongoDB資料庫
- 海量資料相似度計算之simhash短文字查詢
- mongodb查詢MongoDB
- 大資料時代的資料儲存,非關係型資料庫MongoDB大資料資料庫MongoDB
- 關係型資料庫之SQL資料庫SQL
- 關係型資料庫之索引資料庫索引
- Oracle 查詢表與表之間的 主外來鍵關係Oracle
- 【集合論】關係閉包 ( 關係閉包求法 | 關係圖求閉包 | 關係矩陣求閉包 | 閉包運算與關係性質 | 閉包複合運算 )矩陣
- 在EFCore中多對多關係的設計資料插入與查詢
- MongoDB - 聚合查詢MongoDB
- 關於查詢表資料,列與CPU開銷的計算
- 以MongoDB為例與關係型資料庫比較MongoDB資料庫
- MongoDB 關係實現MongoDB
- Linux和大資料雲端計算之間有什麼關係?Linux大資料
- 關於mongodb資料庫的增刪改查MongoDB資料庫