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
- nodejs之MongoDB 非關係型資料庫NodeJSMongoDB資料庫
- MongoDB資料庫中查詢資料(下)MongoDB資料庫
- Mongodb 關聯表查詢MongoDB
- 在MongoDB資料庫中查詢資料(上)MongoDB資料庫
- 關係型資料庫查詢語言 SQL 和圖資料庫查詢語言 nGQL 對比資料庫SQL
- sql運算元據庫(3)-->外來鍵約束、資料庫表之間的關係、三大正規化、多表查詢、事務SQL資料庫
- 聊聊非關係型資料庫MongoDB索引資料庫MongoDB索引
- Python全棧MongoDB資料庫(資料的查詢)Python全棧MongoDB資料庫
- Python全棧 MongoDB 資料庫(資料的查詢)Python全棧MongoDB資料庫
- 20240719資料庫關聯查詢、條件查詢資料庫
- 關係型資料庫之SQL資料庫SQL
- 關係型資料庫之索引資料庫索引
- 在EFCore中多對多關係的設計資料插入與查詢
- MongoDB - 聚合查詢MongoDB
- python資料庫-mongoDB的高階查詢操作(55)Python資料庫MongoDB
- 【集合論】關係閉包 ( 關係閉包求法 | 關係圖求閉包 | 關係矩陣求閉包 | 閉包運算與關係性質 | 閉包複合運算 )矩陣
- MongoDB 關係實現MongoDB
- 海關資料查詢系統「查詢平臺分類」
- 關於mongodb資料庫的增刪改查MongoDB資料庫
- Linux和大資料雲端計算之間有什麼關係?Linux大資料
- 資料庫全表查詢之-分頁查詢優化資料庫優化
- hyperf關聯子表查詢主表資料
- Mongodb高階查詢MongoDB
- MongoDB查詢總結MongoDB
- ❖ MongoDB 高階查詢MongoDB
- mongodb慢查詢分析MongoDB
- MongoDB查詢條件MongoDB
- 反DDD模式之關係型資料庫模式資料庫
- 資料結構之資料、資料元素、資料項、資料物件之間的關係資料結構物件
- day95:flask:SQLAlchemy資料庫查詢進階&關聯查詢FlaskSQL資料庫
- 關於dataguard需要查詢的資料字典
- Python—Django:關於在Django框架中對資料庫的查詢函式,查詢集和關聯查詢PythonDjango框架資料庫函式
- redis實現像關係型資料庫一樣按條件高效查詢分頁Redis資料庫
- 資料庫入門之3張表對比關係型與非關係型資料庫資料庫
- MongoDB慢查詢與索引MongoDB索引
- MongoDB 常用查詢語法MongoDB
- MongoDB查詢內嵌文件MongoDB
- MongoDB日期型別查詢MongoDB型別