MongoDb日常基本操作使用

     在MongoDB中,第一步也是創建數據庫和集合。數據庫用於存儲所有集合,而集合又用於存儲所有文檔。這些文檔將依次包含相關的“字段名”和“字段”值。JSON中的key-value形式。

1、使用數據庫、創建數據庫(相當於集合)

  • 使用“use”命令創建數據庫
  • 使用insert()創建集合/表
  • 使用insert()命令添加文檔
-- 創建數據庫
use  zhw

-- 查看數據庫
show dbs

-- 顯示當前的數據集合(mysql 中叫表)
show collections

-- 刪除集合,刪除指定的集合 刪除表 刪除集合 db.COLLECTION_NAME.drop()

db.zhw.drop()

-- 刪除數據庫,刪除當前所在的數據庫

db.dropDatabase();

    數據庫要想創建成功,那麼必須插入一個數據。數據庫中不能直接插入數據,只能往集合(collections)中插入數據。下面命令表示給 itying 數 據庫的 user 表中插入數據。

2.插入數據 (插入數據,隨着數據的插入,數據庫創建成功了,集合也創建成功了。)

-- 插入一條數據
db.zhw.insert({ item: "card", qty: 15 });
-- 插入多條數據
db.zhw.insertMany( [
      { item: "card", qty: 15 },
      { item: "envelope", qty: 20 },
      { item: "stamps" , qty: 30 }
   ] );

3.查詢所有數據(帶條件和不帶條件)

-- 不帶條件
db.zhw.find();

-- 帶條件
db.zhw.find({"item":"card"});

-- 查詢 qty>20
db.zhw.find({"qty":{$gt:20}})

-- 查詢去掉後的當前聚集集合中的某列的重複數據
db.zhw.distinct("qty");

-- 查詢 qty<22 的記錄
db.zhw.find({qty:{$lt:22}});

-- 查詢 qty>=25 的記錄
db.zhw.find({qty:{$gte:25}});//相當於:select*from zhw where qty>=25;

-- 查詢 qty<=25 的記錄
db.zhw.find({qty:{$lte:25}});

--查詢 qty>=23 並且 qty<=26 注意書寫格式
db.zhw.find({qty:{$gte:23,$lte:26}});

-- 查詢 name 中包含 mongo 的數據 模糊查詢用於搜索
db.zhw.find({name:/mongo/});//相當於%% select * from zhw where name like ‘%mongo%’;

-- 查詢 name 中以 mongo 開頭的
db.zhw.find({name:/^mongo/});//select * from zhw where name like ‘mongo%’;

-- 查詢指定列 name、qty 數據
-- 相當於:select name,qty from zhw ;當然 name 也可以用 true 或 false,
-- 當用 ture 的情況下河 name:1 效果一樣,如果用 false 就 是排除 name,顯示 name 以外的列信息
db.zhw.find({},{name:1,qty :1});

-- 查詢指定列 name、qty 數據,qty >25
-- 相當於:select name,qty from zhw where qty >25;
db.zhw .find({qty :{$gt:25}},{name:1,qty :1});//

-- 按照qty 排序 1 升序 -1 降序
升序: db.zhw.find().sort({qty:1});
降序: db.zhw.find().sort({qty:-1});
-- 查詢 name=zhangsan,qty=22 的數據
--相當於:select * from zhw where name= ‘zhangsan’ and qty= ‘22’;
db.zhw.find({name:'zhangsan',qty:22});

-- 查詢前 5 條數據
db.zhw.find().limit(5);//相當於:select top5* from zhw;

-- 查詢 10 條以後的數據
db.zhw.find().skip(10);

-- 查詢在 5-10 之間的數據 可用於分頁,limit 是 pageSize,skip 是(page-1)*pageSize
db.zhw.find().limit(10).skip(5);

-- or 與 查詢 相當於:select * from zhw where qty=22 or qty=25;
db.zhw.find({$or:[{qty:22},{qty:25}]});

-- findOne 查詢第一條數據 相當於:select top1* from zhw;
db.zhw.findOne();
db.zhw.find().limit(1);

-- 查詢某個結果集的記錄條數 統計數量
--相當於:select count(*) from user where qty>=20;
db.zhw.find({qty:{$gte:25}}).count(); 

-- 如果要返回限制之後的記錄數量,要使用 count(true)或者 count(非 0) db.zhw.find().skip(10).limit(5).count(true);

遊標:

  當使用db.collection.find()函數在集合中搜索文檔時,結果將返回指向文檔集合的指針,該指針稱爲遊標。默認情況下,返回查詢結果時,遊標將自動進行迭代。當然可以一個接一個明確展示遊標中返回的結果目錄。下面的例子,在我們的集合中有3個文檔,光標對象將指向第一個文檔,然後遍歷該集合的所有文檔。

MongoDB中的主鍵:

    在MongoDB中,_id字段是集合的主鍵,以便可以在集合中唯一地標識每個文檔。_id字段包含唯一的ObjectID值。 該主鍵還可以自定義如:

db.zhw.insert({"_id":1, "item": "card", qty: 15 });
db.zhw.find({"_id":1});

4.查詢去掉後的當前聚集集合中的某列的重複數據

db.zhw.distinct("qty");

5.修改數據

-- 更新一條數據
db.zhw.update({"item":"card"},{$set:{"qty":11}});
-- 全部更新
db.zhw.update({"item":"card"},{$set:{"qty":11}},{multi:true});

另一種寫法:

db.zhw.update({item:'card'},{$inc:{qty:50}},false,true); 
相當於:update zhw set qty=qty+50 where item= ‘card’;

db.zhw.update({item:'card'},{$inc:{qty:50},$set:{item:'hoho'}},false,true);
相當於:update zhw set qty=qty+50,item= ‘hoho’ where item= ‘card’;

6.刪除數據

-- 刪除一條數據
db.zhw.remove( {"item":"card"},{justOne:true})

-- 刪除多條數據
db.restaurants.remove( {"item":"card"})

官方文檔:https://docs.mongodb.com/manual/sharding/

社區:https://mongoing.com/archives/docs/mongodb初學者教程/mongodb分片

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章