MongoDB基本操作

來源:互聯網
上載者:User

標籤:odi   error   nbsp   not   moved   tables   control   插入   lin   

1. 進入mongo環境

 mongo

 [[email protected] ~]# mongoMongoDB shell version v3.4.16connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.4.16Server has startup warnings: 2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never‘2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never‘2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] >2.查詢所有資料庫

show dbs;  

> show dbs admin  0.000GBlocal  0.000GB 3.建立或者切換庫(如果資料庫不存在,則建立資料庫,否則切換到指定資料庫)

use mydb; 

> use mydbswitched to db mydb> show dbsadmin  0.000GBlocal  0.000GB

可以看到,我們剛建立的資料庫 mydb並不在資料庫的列表中, 要顯示它,我們需要向 mydb資料庫插入一些資料。

4.建立集合 

> db.createCollection("log"){ "ok" : 1 }> show dbsadmin  0.000GBlocal  0.000GBmydb   0.000GB

5. 插入資料 (如果插入時集合不存在,則包括建立集合和插入資料兩個動作)及更新

> db.logdata.insert({name:‘zhangsan‘, age:‘25‘});WriteResult({ "nInserted" : 1 } > db.logdata.insert({‘_id‘: ‘terrywang‘, ‘super_admin‘: true}) WriteResult({ "nInserted" : 1 }) > db.logdata.find({‘_id‘:‘terrywang‘}){ "_id" : "terrywang", "super_admin" : true } > db.logdata.update({‘_id‘: ‘terrywang‘}, {$set: {‘super_admin‘: false}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.logdata.find({"_id":"terrywang"}){ "_id" : "terrywang", "super_admin" : false } 

6.查看集合內容

> db.logdata.find(){ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }{ "_id" : "terrywang", "super_admin" : false }> db.logdata.findOne({‘_id‘:‘terrywang‘}){ "_id" : "terrywang", "super_admin" : true } 

7.按條件式篩選

> db.logdata.find({"name":"zhangsan"});{ "_id" : ObjectId("5b529e14bfde59a50f5e5396"), "name" : "zhangsan", "age" : "25" }

正則尋找 

> db.logdata.find({"_id":/e*/i}){ "_id" : "terrywang", "super_admin" : false }

   i表明是否是case-insensitive,有i則表示忽略大小寫 

只篩選到某一列 

> db.logdata.find({},{D:1}){ "_id" : ObjectId("5b529ad3aea13d710cc622d5") } 

 8.刪除文檔

先插入一個文檔 

> db.logdata.insert({name:‘lisi‘, age:‘30‘});WriteResult({ "nInserted" : 1 })> db.logdata.find(){ "_id" : ObjectId("5b529e14bfde59a50f5e5396"), "name" : "zhangsan", "age" : "25" }{ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" } 

刪除一個文檔

> db.logdata.remove({"name":"zhangsan"}) 

WriteResult({ "nRemoved" : 1 })> db.logdata.find(){ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }

 9.顯示集合 

> show tables;loglogdata

10.顯示集合中文檔數

> db.logdata.find(){ "_id" : ObjectId("5b529ef8bfde59a50f5e5397"), "name" : "lisi", "age" : "30" }{ "_id" : "terrywang", "super_admin" : false }> db.logdata.count()2

11.查看集合索引

> db.logdata.getIndex()2018-07-21T11:42:09.337+0800 E QUERY    [thread1] TypeError: db.logdata.getIndex is not a function :@(shell):1:1> db.logdata.getIndexes()[        {                "v" : 2,                "key" : {                        "_id" : 1                },                "name" : "_id_",                "ns" : "mydb.logdata"        }] 

12.刪除表 

> db.logdata.drop() true> show tables;logtest> db.test.drop()true> show tableslog

 

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.