目錄
1、 數據庫使用
2、 建立數據庫
3、 插入(增長)數據
4、 查找數據
4、 修改數據
5、 刪除數據
mysql
開啓 mongodb 服務: 要管理數據庫,必須先開啓服務,開啓服務使用 mongod --dbpath
c:\mongodb
管理 mongodb 數據庫: mongo (必定要在新的 cmd 中輸入)
清屏:
sql
cls |
查看全部數據庫列表
mongodb
show dbs |
使用數據庫、 建立數據庫
數據庫
use student |
若是真的想把這個數據庫建立成功, 那麼必須插入一個數據。
數據庫中不能直接插入數據,只能往集合(collections)中插入數據。 不須要專門建立集合,只
須要寫點語法插入數據就會建立集合:
spa
db.student.insert({「name」:」xiaoming」}); |
db.student 系統發現 student 是一個陌生的集合名字,因此就自動建立了集合。
顯示當前的數據集合(mysql 中叫表)
rest
show collections |
刪除數據庫,刪除當前所在的數據庫
orm
db.dropDatabase(); |
刪除集合,刪除指定的集合 刪除表
排序
刪除集合 db.COLLECTION_NAME.drop() db.user.drop() |
插入數據,隨着數據的插入,數據庫建立成功了,集合也建立成功了。
ip
db.表名.insert({"name":"zhangsan"}); student 集合名稱(表) |
1、查詢全部記錄
db.userInfo.find();rem
至關於: select* from userInfo;
2、查詢去掉後的當前彙集集合中的某列的重複數據
db.userInfo.distinct("name");
會過濾掉 name 中的相同數據
至關於: select distict name from userInfo;
3、查詢 age = 22 的記錄
db.userInfo.find({"age": 22});
至關於: select * from userInfo where age = 22;
4、查詢 age > 22 的記錄
db.userInfo.find({age: {$gt: 22}});
至關於: select * from userInfo where age >22;
5、查詢 age < 22 的記錄
db.userInfo.find({age: {$lt: 22}});
至關於: select * from userInfo where age <22;
6、查詢 age >= 25 的記錄
db.userInfo.find({age: {$gte: 25}});
至關於: select * from userInfo where age >= 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/});
//至關於%%
select * from userInfo where name like ‘%mongo%’;
10、查詢 name 中以 mongo 開頭的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%’;
11、查詢指定列 name、 age 數據
db.userInfo.find({}, {name: 1, age: 1});
至關於: select name, age from userInfo;
固然 name 也能夠用 true 或 false,當用 ture 的狀況下河 name:1 效果同樣,若是用 false 就
是排除 name,顯示 name 之外的列信息。
12、查詢指定列 name、 age 數據, age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
至關於: select name, age from userInfo where age >25;
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});
至關於: select * from userInfo where name = ‘zhangsan’ and age = ‘22’ ;
15、查詢前 5 條數據
db.userInfo.find().limit(5);
至關於: selecttop 5 * from userInfo;
16、查詢 10 條之後的數據
db.userInfo.find().skip(10);
至關於: select * from userInfo where id not in (
selecttop 10 * from userInfo
);
17、查詢在 5-10 之間的數據
db.userInfo.find().limit(10).skip(5);
可用於分頁, limit 是 pageSize, skip 是第幾頁*pageSize
18、 or 與 查詢
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
至關於: select * from userInfo where age = 22 or age = 25;
19、 findOne 查詢第一條數據
db.userInfo.findOne();
至關於: selecttop 1 * from userInfo;
db.userInfo.find().limit(1);
20、查詢某個結果集的記錄條數 統計數量
db.userInfo.find({age: {$gte: 25}}).count();
至關於: select count(*) from userInfo where age >= 20;
若是要返回限制以後的記錄數量,要使用 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);
至關於: