## **一、 表操作**
#### 1、查看所有數據庫
~~~
show dbs
~~~
#### 2、使用或創建數據庫
~~~
use student
~~~
> 創建表時默認是沒有大小和文檔數限制的,如果需要創建有大小限制的,可以使用如下
```
db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 })
```
> `capped` (可選) 創建固定集合
> `size` (可選)固定集合指定一個最大值(以字節計)
> `max`(可選)指定固定集合中包含文檔的最大數量
> 查看表結構時候可看到
```
"capped" : true,
"max" : 10000,
"maxSize" : 6142976,
```
#### 3、刪除當前數據庫
~~~
db.dropDatabase()
~~~
#### 4、例舉集合
~~~
show collections
~~~
#### 5、刪除集合
~~~
db.collection.drop()
~~~
#### 6、插入數據
~~~
db.student.insert({"name":"zhangsan"});
~~~
#### 2、查詢集合
## **二、 查找數據**
#### 1、查詢所有記錄
```
db.userInfo.find();
```
> 相當于:select* from userInfo;
#### 2、查詢去掉后的當前聚集集合中的某列的重復數據
```
db.userInfo.distinct("name");
```
> 會過濾掉 name 中的相同數據
> 相當于:select distict name from userInfo;
#### 3、查詢 age = 22 的記錄
```
db.userInfo.find({"age": 22});
```
#### 4、查詢 age > 22 的記錄
```
db.userInfo.find({age: {$gt: 22}});
```
#### 5、查詢 age < 22 的記錄
```
db.userInfo.find({age: {$lt: 22}});
```
#### 6、查詢 age >= 25 的記錄
```
db.userInfo.find({age: {$gte: 25}});
```
#### 7、查詢 age <= 25 的記錄
```
db.userInfo.find({age: {$lte: 25}});
```
#### 8、查詢 age >= 23 并且 age <= 26 注意書寫格式
```
db.userInfo.find({age: {$gte: 23, $lte: 26}});
```
#### 9、查詢 name 中包含 mongo 的數據 模糊查詢用于搜索
```
db.userInfo.find({name: /mongo/});
```
#### 10、查詢 name 中以 mongo 開頭的
```
db.userInfo.find({name: /^mongo/});
```
#### 11、查詢指定列 name、age 數據
~~~
db.userInfo.find({}, {name: 1, age: 1});
~~~
> 當然 name 也可以用 true 或 false,當用 ture 的情況下河 name:1 效果一樣,如果用 false 就 是排除 name,顯示 name 以外的列信息。
#### 12、查詢指定列 name、age 數據, age > 25
~~~
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
~~~
#### 13、按照年齡排序 1 升序 -1 降序
~~~
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});
~~~
#### 14、查詢 name = zhangsan, age = 22 的數據
```
db.userInfo.find({name: 'zhangsan', age: 22});
```
#### 15、查詢前 5 條數據
~~~
db.userInfo.find().limit(5);
~~~
#### 16、查詢 10 條以后的數據
~~~
db.userInfo.find().skip(10);
~~~
#### 17、查詢在 5-10 之間的數據
~~~
db.userInfo.find().limit(10).skip(5);
~~~
> 可用于分頁,limit 是 pageSize,skip 是第幾頁\*pageNumber
#### 18、or 與 查詢
~~~
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
~~~
#### 19、findOne 查詢第一條數據
~~~
db.userInfo.findOne();
~~~
#### 20、查詢某個結果集的記錄條數 統計數量
~~~
db.userInfo.find({age: {$gte: 25}}).count();
~~~
> 如果要返回限制之后的記錄數量,要使用 count(true)或者 count(非 0) db.users.find().skip(10).limit(5).count(true);
## **三、修改數據**
修改里面還有查詢條件。你要改誰,要告訴 mongo。
查找名字叫做小明的,把年齡更改為 16 歲:
~~~
db.student.update({"name":"小明"},{$set:{"age":16}});
~~~
查找數學成績是 70,把年齡更改為 33 歲:
~~~
db.student.update({"score.shuxue":70},{$set:{"age":33}});
~~~
更改所有匹配項目:" By default, the update() method updates a single document. To update multiple documents, use the multi option in the update() method.
~~~
db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});
~~~
完整替換,不出現$set 關鍵字了: 注意
~~~
db.student.update({"name":"小明"},{"name":"大明","age":16});
~~~
```
db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);
```
相當于:update users set age = age + 50 where name = ‘Lisi’;
`db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);`
相當于:update users set age = age + 50, name = ‘hoho’ where name = ‘Lisi’;
## **四、 刪除數據**
~~~
db.collectionsNames.remove( { "borough": "Manhattan" } )
db.users.remove({age: 132});
~~~
By default, the remove() method removes all documents that match the remove condition. Use the justOne option to limit the remove operation to only one of the matching documents.
~~~
db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )
~~~