Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB basic command operation

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In the previous essay on how to install the MongoDB database on Centos7, let's learn the basic operation commands together.

After the installation is completed, you can log in to the database without a password by typing mongo interactively with shell.

Show dbs; # View all databases

Show collections; # displays collections in the current database (similar to tables in a relational database)

Show users; # View the user information of the current database

Use; # switching databases is the same as mysql

Db; or db.getName (); # View the current database

Db.help (); # shows database operation commands, with many commands in it

Db.foo.help (); # shows collection operation commands, and there are also many commands. Foo refers to a collection called foo under the current database, not a real command.

Db.foo.find (); # perform a data lookup for the foo collection in the current database (all data will be listed because there are no conditions)

Db.foo.find ({a: 1}); # looks up the foo collection in the current database if there is an attribute in the data called an and the value of an is 1

Create an example of a test database:

Use test; # create a database

Switched to db test

Db

Test

Show dbs; # check the database

Admin 0.000GB

Local 0.000GB

Db.test.insert ({"_ id": "520", "name": "xiaoming"}) # create a table

WriteResult ({"nInserted": 1})

Db.createUser ({user: "xiaoming", pwd: "123456", roles: [{role: "userAdmin", db: "test"}]}) # create a user

Successfully added user: {

"user": "xiaoming"

"roles": [

{

"role": "userAdmin"

"db": "test"

}

]

}

Db.removeUser ("userName"); # Delete user

Show users; # shows all current users

Db.dropDatabase (); # Delete the currently used database

Show dbs

Admin 0.000GB

Local 0.000GB

Test 0.000GB

Test_1 0.000GB

Db

Test_1

Db.dropDatabase ()

{"dropped": "test_1", "ok": 1}

Show dbs

Admin 0.000GB

Local 0.000GB

Test 0.000GB

Db.stats (); # displays the current db status

Db.stats ()

{

"db": "test_1"

"collections": 0

"views": 0

"objects": 0

"avgObjSize": 0

"dataSize": 0

"storageSize": 0

"numExtents": 0

"indexes": 0

"indexSize": 0

"fileSize": 0

"ok": 1

}

Db.version (); # current db version

Db.version ()

3.4.10

Db.getMongo (); # View the address of the linked machine of the current db

Db.getMongo ()

Connection to 172.16.40.205:27017

Enable remote access:

Edit configuration file: vi / etc/mongod.conf

BindIp: 172.16.40.205 # server IP address where the database is located

Save and restart the database!

Local login: mongo 172.16.40.205/admin-uadmin-p123456

Remote login:

Download the mongodb package

Mongodb-linux-x86_64-3.4.10.tgz decompression

Tar zxvf mongodb-linux-x86_64-3.4.10.tgz enters the bin directory

Cd mongodb-linux-x86_64-3.4.10/bin connects to a remote database

. / mongo 172.16.40.205:27017/admin-u user-p password

That's all for now, and we'll continue to add later.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report