日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

MongoDB的常用命令匯總(Mongo4.2.8)_MongoDB

作者:fen_fen ? 更新時間: 2022-03-27 編程語言

一、數(shù)據(jù)庫相關

1.切換/創(chuàng)建數(shù)據(jù)庫

>use “dbname”;

2.查詢所有數(shù)據(jù)庫

> show dbs;
mytest ?0.000GB

3.查看當前使用的數(shù)據(jù)庫

> db.getName();

Mytest

4.查看數(shù)據(jù)庫版本

> db.version();

4.2.8

5.查看當前db的鏈接地址

> db.getMongo();

connection to 127.0.0.1:27017

二、用戶相關

1、創(chuàng)建普通用戶(創(chuàng)建用戶cg,對mytest數(shù)據(jù)庫讀寫權限)

> db.createUser({user:"cg",pwd:"lianshi",roles:[{role:"readWrite",db:"mytest"}]})

2、刪除用戶>db.dropUser("yonghu")

3、修改用戶密碼

db.updateUser("cg",{pwd:"123456"})

4、進入數(shù)據(jù)mytest,用戶名密碼認證

> db.auth("cg","lianshi");

三、集合Collection相關

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" }
相當于:select* from student;

4.查詢唯一字段值

> db.student.distinct("name");
[ "hmf", "yx", "yxy" ]

會過濾掉name中的相同數(shù)據(jù)
相當于: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" }

相當于: 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、查詢語句

db.student.find({})
???.projection({})
???.sort({_id:-1})
???.limit(100)

10、刪除(集合)表

db.student.drop();

參考:https://www.jb51.net/article/48217.htm

原文鏈接:https://blog.csdn.net/fen_fen/article/details/106906344

欄目分類
最近更新