MongoDB配置與基礎用法

來源:互聯網
上載者:User

標籤:匯入資料   環境變數   update   doc   管理   檔案夾   ffffff   精確   資料庫名   

MongoDB 安裝

官網:https://www.mongodb.com/

手冊:https://docs.mongodb.org/manual/

win7系統需要安裝補丁,KB2731284

  • 安裝完成配置環境變數:
  • C:\Program Files\MongoDB\Server\3.0\bin 加入到系統的path環境變數中
基礎命令
  • gongod: 開機
  • mongoimport 匯入資料
  • mongo 使用資料庫,運行此命令後的環境就是mongo文法了
  • show dbs:列出所有資料庫
  • use 資料庫名字:使用某個資料庫
  • db:查看當前所在資料庫

注意:如果use一個不存在的資料庫就是建立,但是只有執行插入資料語句後才能建立成功。

資料庫的使用

要管理資料庫,必須先開機,開機使用mongod --dbpath c:\mongom命令(--dbpath就是選擇資料庫文檔所在的檔案夾)

1.插入資料
  • 普通插入

    db.student.insert({"name":"xiaoming"});

  • 匯入資料

    /* * db test 想往哪個資料庫裡面匯入 * collection restaurants 想往哪個集合中匯入 * drop 把集合清空 * file primer-dataset.json 哪個檔案 */ mongoimport --db test --collection restaurants --drop --file primer-dataset.json
2.查詢資料
  • 查詢全部

    db.restaurants.find()

  • 精確匹配

    db.student.find({"score.shuxue":80});

  • 多個條件

    db.student.find({"score.shuxue":80 , "age":22})

  • 大於條件

    db.student.find({"score.yuwen":{$gt:60}});

  • db.student.find({$or:[{"age":18},{"age":22}]});

  • 排序

    db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )

3.修改資料
  • 單條資料修改

    db.student.update({"name":"小明"},{$set:{"age":22}});

  • 更改多條匹配資料(加上multi參數)

    db.student.update({"score.shuxue":80},{$set:{"age":26}},{multi: true});

  • 完成替換(不加$set關鍵字)

    db.student.update({"name":"小明"},{"name":"大明","age":28});

4.刪除資料
  • db.restaurants.remove( { "borough": "Manhattan" } )

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.