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 commonly used commands in MongoDB database

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

Share

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

In this issue, the editor will bring you about the commonly used commands in the MongoDB database. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

1, MongoDB database common operation commands 1, Help view command prompt helpdb.help (); db.yourColl.help (); 2, switch / create database use raykaeso

The current database is automatically created when a collection (table) is created

3. Query all databases show dbs;4, delete the currently used database db.dropDatabase (); 5. Clone the database db.cloneDatabase from the specified host ("127.0.0.1")

Clone the data from the database on the specified machine to the current database

6. Copy the specified database data from the specified machine to a database db.copyDatabase ("mydb", "temp", "127.0.0.1")

Copy the data of native mydb to temp database

7. Repair the current database db.repairDatabase (); 8, view the currently used database db.getName () / db;9, display the current db status db.stats (); 10, the current db version db.version (); 11, view the current db connection server machine address db.getMongo (); 12, query the previous error message and clear db.getPrevError (); db.resetError () 2. MongoDB Collection aggregation 1. Create a table db.createCollection ("collName", {size: 20, capped: 5, max: 100}); / / display {"ok": 1} / / determine whether the collection is a fixed capacity db.collName.isCapped (); 2. Get the specified aggregate set (table) db.getCollection ("account"); 3. Get all the aggregation sets of the current db () 4. Display the status of all the clustered indexes in the current db db.printCollectionStats (); 5, query the number of data items in the current collection db.yourColl.count (); 6, view the data space size of the current collection db.yourColl.dataSize (); 7, get the dbdb.yourColl.getDB () where the current clustered set is located; 8, get the current clustered state db.coll.stats (); 9, get the total size of the clustered set db.coll.totalSize () 10. Db.coll.storageSize (); 11. Rename db.coll.renameCollection ("ray")

Rename coll to ray

12. Delete the current aggregate collection db.coll.drop (); 3. MongoDB user related 1. Add a user (create) db.createUser ({user: 'username', pwd:' xxxx', roles: [{role: 'readWrite', db:' dbname'}]})

Add users, set passwords, whether read-only

2. Database authentication, security mode (login) db.auth ("ray", "123456"); 3, display all current user show users;4, delete user db.removeUser ("userName"); 4. MongoDB aggregate collection query 1, query all records db.userInfo.find ()

Equivalent to: select* from userInfo

By default, 20 records are displayed per page, and when the display is not low, you can use the it iterative command to query the next page of data. Note: you cannot type the it command with ";"

But you can set the size of the data displayed on each page, using DBQuery.shellBatchSize= 50; that will show 50 records per page.

2. Query the duplicate data db.userInfo.distinct ("name") of a column in the current aggregate set after the query is removed

Will filter out the same data in name

Equivalent to: select distict name from userInfo

3. Query the record db.userInfo.find of age = 22 ({"age": 22})

Equivalent to: select * from userInfo where age = 22

4. Records of conditional queries

The conditional operators in MongoDB are:

(>) greater than-$gt

(=) greater than or equal to-$gte

Find ({age: {$lt: 22}); equivalent to: select * from userInfo where age= 25: 6, character ambiguity query db.userInfo.find ({name: / mongo/}); / / equivalent to% select * from userInfo where name like'% mongo%';7, query specified column data db.userInfo.find ({}, {name: 1, age: 1}); equivalent to: select name, age from userInfo

Of course, name can also use true or false

8. Query the specified column data db.userInfo.find ({age: {$gt: 25}}, {name: 1, age: 1}) by condition; equivalent to: select name, age from userInfo where age = 20; V, MongoDB index 1, create index db.userInfo.ensureIndex ({name: 1}); db.userInfo.ensureIndex ({name: 1, ts:-1}); 2. Query all indexes in the current clustered set db.userInfo.getIndexes () 3. View the total index record size db.userInfo.totalIndexSize (); 4, read all the index information of the current collection db.users.reIndex (); 5, delete the specified index db.users.dropIndex ("name_1"); 6, delete all index db.users.dropIndexes (); 6. MongoDB modify, add, delete collection data 1, add db.users.save ({name: 'zhangsan', age: 25, sex: true})

The data column of the added data, which is not fixed, according to the added data.

2. Modify db.users.update ({age: 25}, {$set: {name: 'changeName'}}, false, true); equivalent to: update users set name =' changeName' where age = 25 name db.users.update ({name: 'Lisi'}, {$inc: {age: 50}}, false, true); equivalent to: update users set age = age + 50 where name =' Lisi' Db.users.update ({name: 'Lisi'}, {$inc: {age: 50}, $set: {name:' hoho'}}, false, true); equivalent to: update users set age = age + 50, name = 'hoho' where name =' Lisi';3, delete db.users.remove ({age: 132}) 4. Query, modify and delete db.users.findAndModify ({query: {age: {$gte: 25}}, sort: {age:-1}, update: {$set: {name: 'a2'}, $inc: {age: 2}}, remove: true}). These are the commonly used commands in the MongoDB database shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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