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

What are the basic commands of mongodb

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

Share

Shulou(Shulou.com)05/31 Report--

What are the basic commands of mongodb? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1 switch database

Use admin

2 View the current database

Db

2.1 View all databases

Show dbs

3 View the tables under the current database

Show collections

4 Delete the database

Use test;db.dropDatabase (); # Delete the current database show dbs; # verify the deletion result

5 Delete a collection or table

Db.table_name.drop (); # Delete table table_name

6 View account information

Mongo-port=23000use admin;db.auth ('username','password') db.system.users.find (). Pretty () # View all account information show users; # View current account information db.getUser (' username') # View the information of the specified user

7 backup and restore using gzip compression. Just change the command, and the rest will remain the same. There is no need to set the user name and password here.

Mongodump-- port=23000-- archive=/data/mongo_backup/testdb-2.20191203.gz-- db testdb-2-- gzipmongorestore-- port=23000-- archive=/data/mongo_backup/testdb-2.20191203.gz-- db testdb-2-- gzip** this backup method generates a gzip file, which is also a file after decompression, and all the contents are put into one file.

8 backup without compression

Mongodump-- port=23000-- db=testdb-2-o / data/mongo_backup/20191203mongorestore-- port=23000-- db=testdb-2-- drop / data/mongo_backup/20191203/testdb-2** is backed up in this way, usually with two files per table: .bson and .metadata.json

9 use the mongo command to query in shell

# use eval mongo ip:port/database-- eval "[root@localhost ~] # mongo localhost:23000/testdb-2-- eval" printjson (db.table1.findOne ()) "MongoDB shell version v3.6.13connecting to: mongodb://localhost:23000/testdb-2?gssapiServiceName=mongodbImplicit session: session {" id ": UUID (" e87ef5ae-a6b7-47d6-a91c-65f3a0b81ac0 ")} MongoDB server version: 3.6.13 {" _ id ": ObjectId (" 5de60a767321940034390f16 ")," id ": 129 "name": "hehe"} # use-- quiet to remove unnecessary information [root@localhost ~] # mongo localhost:23000/testdb-2-- quiet-- eval "printjson (db.table2.findOne ())" {"_ id": ObjectId ("5de615b8eac07a724c6911b6"), "id": 6, "name": "hehe"} # Delete table [root@localhost ~] # mongo localhost:23000/testdb-2-- eval "db.table1.drop () "MongoDB shell version v3.6.13connecting to: mongodb://localhost:23000/testdb-2?gssapiServiceName=mongodbImplicit session: session {" id ": UUID (" 0cf5b11f-c6ef-417c-8bbd-a2f8414f589c ")} MongoDB server version: 3.6.13true

Use the mongo command in 10 shell scripts

#! / bin/bashmongo localhost:23000/testdb-2

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