一葛家、 數(shù)據(jù)庫使用
開啟 mongodb 服務:要管理數(shù)據(jù)庫沟蔑,必須先開啟服務,開啟服務使用 mongod --dbpath c:\mongodb
管理 mongodb 數(shù)據(jù)庫:mongo (一定要在新的 cmd 中輸入)
清屏:
cls
查看所有數(shù)據(jù)庫列表
show dbs
二、 創(chuàng)建數(shù)據(jù)庫
使用數(shù)據(jù)庫遗遵、創(chuàng)建數(shù)據(jù)庫
use student
如果真的想把這個數(shù)據(jù)庫創(chuàng)建成功,逸嘀。
數(shù)據(jù)庫中不能直接插入數(shù)據(jù)车要,只能往集合(collections)中插入數(shù)據(jù)。不需要專門創(chuàng)建集合崭倘,只需要寫點語法插入數(shù)據(jù)就會創(chuàng)建集合:
db.student.insert({“name”:”xiaoming”});
db.student 系統(tǒng)發(fā)現(xiàn) student 是一個陌生的集合名字翼岁,所以就自動創(chuàng)建了集合。
顯示當前的數(shù)據(jù)集合(mysql 中叫表)
show collections
刪除數(shù)據(jù)庫司光,刪除當前所在的數(shù)據(jù)庫
db.dropDatabase();
刪除集合琅坡,刪除指定的集合刪除表
刪除集合 db.COLLECTION_NAME.drop()
db.user.drop()
三、 插入(增加)數(shù)據(jù)
插入數(shù)據(jù)残家,隨著數(shù)據(jù)的插入榆俺,數(shù)據(jù)庫創(chuàng)建成功了,集合也創(chuàng)建成功了坞淮。
四茴晋、 查找數(shù)據(jù)
1、查詢所有記錄
db.userInfo.find();
*相當于:*select* from userInfo;
2回窘、查詢?nèi)サ艉蟮漠斍熬奂现械哪沉械闹貜蛿?shù)據(jù)
db.userInfo.distinct("name");
*會過濾掉* name *中的相同數(shù)據(jù)*
*相當于:*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的數(shù)據(jù)模糊查詢用于搜索
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 數(shù)據(jù)
db.userInfo.find({}, {name: 1, age: 1});
*相當于:*select name, age from userInfo;
*當然* name *也可以用* true *或* false,*當用* ture *的情況下河* name:1 *效果一樣,如果用* false *就**是排除* name*掂榔,顯示* name *以外的列信息继效。*
12 、查詢指定列 name 装获、 age 數(shù)據(jù) , 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 的數(shù)據(jù)
db.userInfo.find({name: 'zhangsan', age: 22});
*相當于:*select * from userInfo where name = *‘*zhangsan*’* and age = *‘*22*’*;
15穴豫、查詢前 5 條數(shù)據(jù)
相當于:selecttop 5 * from userInfo;
16凡简、查詢 10 條以后的數(shù)據(jù)
db.userInfo.find().skip(10);
相當于:select * from userInfo where id not in (
selecttop 10 * from userInfo
);
17逼友、查詢在 5-10 之間的數(shù)據(jù)
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 查詢第一條數(shù)據(jù)
db.userInfo.findOne();
相當于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);
20筐眷、查詢某個結果集的記錄條數(shù) 統(tǒng)計數(shù)量
db.userInfo.find({age: {$gte: 25}}).count();
相當于:select count(*) from userInfo where age >= 20;
如果要返回限制之后的記錄數(shù)量黎烈,要使用 count(true)或者 count(非 0)
db.users.find().skip(10).limit(5).count(true);
四、修改數(shù)據(jù)
修改里面還有查詢條件匀谣。你要該誰照棋,要告訴 mongo。
查找名字叫做小明的武翎,把年齡更改為 16 歲:
1 db.student.update({"name":"小明"},{$set:{"age":16}});
查找數(shù)學成績是 70烈炭,把年齡更改為 33 歲:
1 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.
1 db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});
完整替換,不出現(xiàn)$set 關鍵字了: 注意
1 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’;
五宝恶、 刪除數(shù)據(jù)
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 } )