MongoDB學習筆記

來源:互聯網
上載者:User

標籤:tags   group by   tty   insert   學習   desc   格式   文檔   ade   

1、每次啟動需要配置開機檔案  mongod --dbpath G:\data\db  執行完上面命令再開啟一個新的視窗執行 mongo 2、show dbs #查看所有資料庫  use mydb #使用mydb資料庫  show collections #查看該資料庫下所有的表 3、MongoDB中的查詢  db.col.find() #查看col集合下所有的document(document相當於mysql中的row)  db.col.find().pretty() #以格式化的方式來顯示所有文檔  db.col.find({"by":"菜鳥教程"}) #條件查詢相當於sql中 where by = ‘菜鳥教程‘  db.col.find({"likes":{$lt:50}}) # where likes < 50  db.col.find({"likes":{$lte:50}}) #where likes <= 50  db.col.find({"likes":{$gt:50}}) # where likes > 50  db.col.find({"likes":{$gte:50}}) #where likes >= 50  db.col.find({"likes":{$ne:50}}) #where likes != 50  db.col.find({"by":"菜鳥教程", "title":"MongoDB 教程"}) #WHERE by=‘菜鳥教程‘ AND title=‘MongoDB 教程‘  db.col.find({$or:[{"by":"菜鳥教程"},{"title": "MongoDB 教程"}]}) #WHERE by=‘菜鳥教程‘ OR title=‘MongoDB 教程‘  db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鳥教程"},{"title": "MongoDB 教程"}]}) #where likes>50 AND (by = ‘菜鳥教程‘ OR title = ‘MongoDB 教程‘) 4、MongoDB中的刪除  db.col.remove({‘title‘:‘MongoDB 教程‘}) #移除 title 為 ‘MongoDB 教程‘ 的文檔  db.col.remove({}) #刪除col中的資料 5、兩種MongoDB中的插入  db.col.insert({title: ‘MongoDB 教程‘, description: ‘MongoDB 是一個 Nosql 資料庫‘, by: ‘菜鳥教程‘, url: ‘http://www.runoob.com‘, tags: [‘mongodb‘, ‘database‘, ‘NoSQL‘], likes: 100 })   document=({title: ‘MongoDB 教程‘, description: ‘MongoDB 是一個 Nosql 資料庫‘, by: ‘菜鳥教程‘, url: ‘http://www.runoob.com‘, tags: [‘mongodb‘, ‘database‘, ‘NoSQL‘], likes: 100 });  db.col.insert(document) 6、MongoDB中的更新  db.col.update({‘title‘:‘MongoDB 教程‘},{$set:{‘title‘:‘MongoDB‘}}) #通過 update() 方法來更新標題(title)   #save() 方法通過傳入的文檔來替換已有文檔  db.col.save({     "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一個 Nosql 資料庫", "by" : "Runoob", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 }) #替換了 _id 為 56064f89ade2f21f36b03136 的文檔資料 7、MongoDB中的排序  使用sort()方法對資料進行排序,sort()方法可以通過參數指定排序的欄位,其中 1 為升序排列,而-1是用於降序排列  db.col.find({},{"title":1,_id:0}).sort({"likes":-1}) #col 集合中的資料按欄位 likes 的降序排列 8、MongoDB中的索引  db.col.ensureIndex({"title":1}) #為title欄位建立索引,1為升序,2為降序  db.col.ensureIndex({"title":1,"description":-1}) #使用多個欄位建立索引(關係型資料庫中稱作複合索引) 9、MongoDB中的彙總(aggregate)  主要用於處理資料(諸如統計平均值,求和等),並返回計算後的資料結果。有點類似sql語句中的 count(*)  db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])  #集合計算每個作者所寫的文章數,$sum計算總和,類似於select by_user as _id, count(*) as num_tutorial from mycol group by by_user 

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.