MongoDB 資料庫建立刪除、表建立刪除、資料增刪改查

來源:互聯網
上載者:User

標籤:pos   mit   包含   資料庫   ike   去掉   rest   增刪改查   out   

一、管理 mongodb 資料庫:mongo 

查看所有資料庫列 表 

show dbs

二、 建立資料庫 

建立 資料庫

use student

如果真的想把這個資料庫建立成功,(collections)中插入數 據。不需要專門建立集合,只db.student 系統發現 是一個陌生的集合名字,所以就顯示當前的資料集合(中叫表)刪除集合,刪除指定的集合 1、查詢所有記 錄

db.userInfo.find();
相當於:select* from userInfo;
2、查詢去掉後 的當前聚集集合中的某列的重複資料

db.student.insert({“name”:”x iaom ing”});

show collections

db.dropDatabase();

刪除集合 db.COLLECTION_NAME.drop() db.user.drop()

db.表名.insert({"name":"zhangsan"}

);

student 集合名稱(表)

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 <= 26db.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 > 25db.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. userI nfo. fin d(). limit (1 0). 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 歲:

尋找數學成績是 70,把年齡更改為 33 歲:

更改所有匹配項目:"
By default, the update() method updates a single document. To update multiple documents, usethe multi option in the update() method.

完整替換,$set 注意

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’;

五、 刪除資料

1 db.student.update({"name":"小明"},{$set:{"ag e":16}});

1 db.student.update({"sc ore.shuxue":70},{$set:{"ag e":33}});

1 db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});

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

db.collectionsNames.remove( { "borough": "Manhattan" } )

db.users.remove({age: 132});

By default, the remove() method removes all documents that match the remove condition. Usethe justOne option to limit the remove operation to only one of the matching documents.

db.restaurants.remove( { "borough": "Queens" }, { justOne: true } ) 

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.