MongoDB的常用命令匯總(Mongo4.2.8)
一、數(shù)據(jù)庫(kù)相關(guān)
1.切換/創(chuàng)建數(shù)據(jù)庫(kù)
>use “dbname”;
2.查詢所有數(shù)據(jù)庫(kù)
> show dbs; mytest ?0.000GB
3.查看當(dāng)前使用的數(shù)據(jù)庫(kù)
> db.getName();
Mytest
4.查看數(shù)據(jù)庫(kù)版本
> db.version();
4.2.8
5.查看當(dāng)前db的鏈接地址
> db.getMongo();
connection to 127.0.0.1:27017
二、用戶相關(guān)
1、創(chuàng)建普通用戶(創(chuàng)建用戶cg,對(duì)mytest數(shù)據(jù)庫(kù)讀寫權(quán)限)
> db.createUser({user:"cg",pwd:"lianshi",roles:[{role:"readWrite",db:"mytest"}]})
2、刪除用戶>db.dropUser("yonghu")
3、修改用戶密碼
db.updateUser("cg",{pwd:"123456"})
4、進(jìn)入數(shù)據(jù)mytest,用戶名密碼認(rèn)證
> db.auth("cg","lianshi");
三、集合Collection相關(guān)
1.獲得數(shù)據(jù)聚合(表)
> db.getCollectionNames(); [ "student" ]
2. 集合(表)插入數(shù)據(jù)
db.student.insert({"id":"2","name":"yxy"})
3.查詢數(shù)據(jù)
> db.student.find(); { "_id" : ObjectId("5eef61f3447efbc4346fbb9b"), "id" : "2", "name" : "yxy" } { "_id" : ObjectId("5eef61fe447efbc4346fbb9c"), "id" : "1", "name" : "hmf" } { "_id" : ObjectId("5eeff9582e8cdcf5c32c0ecf"), "id" : "3", "name" : "yx" } 相當(dāng)于:select* from student;
4.查詢唯一字段值
> db.student.distinct("name"); [ "hmf", "yx", "yxy" ]
會(huì)過濾掉name中的相同數(shù)據(jù)
相當(dāng)于:select distict name from student;
5.查詢name = yxy的記錄
> db.student.find({"name":"yxy"}); { "_id" : ObjectId("5eef61f3447efbc4346fbb9b"), "id" : "2", "name" : "yxy" } { "_id" : ObjectId("5ef077145c4ca32ccc787893"), "id" : "2", "name" : "yxy" }
相當(dāng)于: select * from student where name = “yxy”;
6.插入int32字段類型的數(shù)據(jù)
db.student.insert({"id":NumberInt(1234567),"name":"hu"});
7、插入int64字段類型數(shù)據(jù)
db.student.insert({"age":NumberLong(22),"name":"hu"});
8、插入Decimal字段類型數(shù)據(jù)
db.student.insert({"va":NumberDecimal("22.3"),"name":"hu"});
9、查詢語(yǔ)句
db.student.find({}) ???.projection({}) ???.sort({_id:-1}) ???.limit(100)
10、刪除(集合)表
db.student.drop();
參考:http://www.dbjr.com.cn/article/48217.htm
到此這篇關(guān)于MongoDB的常用命令匯總(Mongo4.2.8)的文章就介紹到這了,更多相關(guān)MongoDB常用命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
NoSQL優(yōu)缺點(diǎn)與MongoDB數(shù)據(jù)庫(kù)簡(jiǎn)介
這篇文章介紹了NoSQL的優(yōu)缺點(diǎn)與MongoDB數(shù)據(jù)庫(kù),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06MongoDB學(xué)習(xí)筆記—Linux下搭建MongoDB環(huán)境
本篇文章主要介紹了Linux下搭建MongoDB環(huán)境,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-11-11mongodb 數(shù)據(jù)庫(kù)操作詳解--創(chuàng)建,切換,刪除
mongodb是nosql里面最像關(guān)系型數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù)。單表操作,基本上可以和關(guān)系型數(shù)據(jù)庫(kù)差不多。mongodb比較易學(xué),易用,分幾期記錄一下,學(xué)習(xí)和使用mongodb過程。2014-07-07MongoDB設(shè)置登錄賬號(hào)、密碼及權(quán)限的詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于MongoDB設(shè)置登錄賬號(hào)、密碼及權(quán)限的詳細(xì)過程,文中通過代碼以及圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MongoDB具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09MongoDb的"not master and slaveok=false"錯(cuò)誤及解決方法
今天小編就為大家分享一篇關(guān)于MongoDb的"not master and slaveok=false"錯(cuò)誤及解決方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10