MongoDB數據庫常用指令

   mongod:啓動MongoDB服務器

   mongo:進入數據庫

   mongod --dbpath 路徑 --port 端口號:指定數據庫端口
   例:mongod --dbpath C:\user\data\db --port 123

   show dbs:顯示當前所有的數據庫

   use 數據庫名稱:進入到指定數據庫中(或創建新數據庫)

   db:顯示當前所在的數據庫

   show collections:顯示當前數據庫中所有集合

   db.<collection>.insert(doc):向當前數據庫中插入<collection>集合
   例:向school數據庫中插入學生student集合:
   db.student.insert({id: "001", name: "jack",age:18});

   db.<collection>.find():查詢當前集合中所有的文檔
   例:查詢student集合中的所有文檔
   db.student.find();
   查詢年齡在18歲的所有同學
   db.student.find({age:18});
   查詢年齡在18歲的一個同學
   db.student.findOne({age:18});
   查詢年齡在18歲的人數
   db.student.find({age:18}).count();
   查詢年齡在18歲的數據長度
   db.student.find({age:18}).length();

   更新一個名字爲jack的年齡屬性
   db.student.update({"name":"jack"},{$set:{"age":50}});
   刪除一個名字爲jack的年齡屬性
   db.student.update({"name":"jack"},{$unset:{"age":50}});
   更新所有性別爲女的hobby屬性
   db.student.updateMany({"sex":"女"},{$set:{
      "hobby":"編程"
   }});

   刪除年齡在20歲的學生,true爲刪除一條,默認爲false刪除多條
   db.student.remove({"age":20},true);
   刪除整個student集合
   db.student.remove({});

   插入一萬條文檔
   var arr = [];
   for(var i=0;i<10000;i++){
     arr.push({counter:i+1});
   }
   db.demos.insert(arr);
   查詢集合demos裏counter小於666的文檔
   db.demos.find({counter:{$lt:666}});$lte小於等於10
   查詢集合demos裏counter大於666的文檔
   db.demos.find({counter:{$gt:666}});$gte大於等於10
   查詢集合demos裏counter大於66且小於666的文檔
   db.demos.find({counter:{$gt:66,$lt:666}});
   查詢集合demos裏counter大於66或小於666的文檔
   db.demos.find({$or:[{wages: {$lt:66}},{wages:{$gt:666}}]});
   查詢集合demo中前十條數據
   db.demos.find().limit(10);
   查詢集合demo中第11到第20條數據skip(10)從11開始,limit(10)限制查詢數據爲10條
   db.demos.find().skip(10).limit(10);
   對demos裏的counter排序(-1:降序,1:升序)
   db.demos.find().sort({counter:-1});

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