MongoDB database entry database creation, deletion, table (set) creation and deletion, data addition, deletion, modification and query

1, Connect database

1. Connect database

2. View a list of all databases

show dbs

2, Create database, view and delete database

1. Use database, create database

use Database name

If you really want to create this database successfully, you must insert a data.

Data cannot be directly inserted into the database. Only data can be inserted into collections. The following command indicates to insert data into the user table of xxx database.

db.user.insert({"name":"xiaoming"});

2. View database

show dbs

3. Display the current data set (called table in mysql)

show collections

4. Delete set, delete specified set, delete table

Delete collection db.Set name.drop() 
db.user.drop()

5. Delete the database and delete the current database

db.dropDatabase();

3, Insert (add) data

Insert data. With the insertion of data, the database and collection are created successfully.

db.Table name.insert({"name":"zhangsan","age":20});

Insert multiple pieces of data

4, Find data

1. Query all records

db.user.find();
amount to: select* from user;

2. Query the duplicate data of a column in the current aggregate set after removal

db.user.distinct("name"); 
Will filter out name Same data in 
amount to: select distict name from user;

3. Query records with age = 22

db.user.find({"age": 22}); 
amount to: select * from user where age = 22;

4. Query records with age > 22

db.user.find({age: {$gt: 22}}); 
amount to: select * from user where age >22;

5. Query records with age < 22

db.user.find({age: {$lt: 22}}); 
amount to: select * from user where age <22;

6. Query records with age > = 25

db.user.find({age: {$gte: 25}}); 
amount to: select * from user where age >= 25;

7. Query records with age < = 25

db.user.find({age: {$lte: 25}});
amount to: select * from user where age <= 25;

8. Query age > = 23 and age < = 26. Pay attention to the writing format

db.user.find({age: {$gte: 23, $lte: 26}});

9. Query name contains mongo's data. Fuzzy query is used for search

db.user.find({name: /mongo/}); 
amount to%% 
select * from user where name like '%mongo%';

10. Query name s starting with mongo

db.user.find({name: /^mongo/}); 
amount to select * from user where name like 'mongo%';

11. Query the name and age data of the specified column

db.user.find({}, {name: 1, age: 1}); 
amount to: select name, age from user; 
of course name It can also be used true or false,When used ture River case name:1 The effect is the same, if you use false Is to exclude name,display name Column information other than.

12. Query the name and age data of the specified column, age > 25

db.user.find({age: {$gt: 25}}, {name: 1, age: 1}); 
amount to: select name, age from user where age >25;

13. Sort by age 1 ascending - 1 descending

Ascending order: 
db.user.find().sort({age: 1});
Descending order: 
db.user.find().sort({age: -1})

14. Query the data with name = Zhangsan and age = 22

db.user.find({name: 'zhangsan', age: 22}); 
amount to: select * from user where name = 'zhangsan' and age = '22';

15. Query the first 5 data

db.user.find().limit(5); 
amount to: selecttop 5 * from user;

16. Query data after 10 items

db.user.find().skip(10);

17. Query data between 5-10

db.user.find().limit(10).skip(5);
Can be used for paging, limit yes pageSize,skip yes(page-1)*pageSize

18. or and query

db.user.find({$or: [{age: 22}, {age: 25}]}); 
amount to: select * from user where age = 22 or age = 25;

19. findOne queries the first data

db.user.findOne(); 
amount to: selecttop 1 * from user; 
db.user.find().limit(1);

20. Query the number of records in a result set

db.user.find({age: {$gte: 25}}).count(); 
amount to: select count(*) from user where age >= 20; 
If you want to return the number of records after the limit, use count(true)perhaps count(Not 0) db.users.find().skip(10).limit(5).count(true);

5, Modify data

There are query criteria in the modification. Who do you want to change? Tell mongo.

1. Find the one named Xiao Ming and change the age to 16:

db.student.update({"name":"Xiao Ming"},{$set:{"age":16}});

2. Find that your math score is 70 and change your age to 33:

db.student.update({"score.shuxue":70},{$set:{"age":33}});

3. Change all matching items

db.student.update({"sex":"male"},{$set:{"age":33}},{multi: true});

4. Complete replacement without $set keyword

db.student.update({"name":"Xiao Ming"},{"name":"Daming","age":16});

5,inc

db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true); 
amount to: update users set age = age + 50 where name = 'Lisi';
db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true); 
amount to: update users set age = age + 50, name = 'hoho' where name = 'Lisi';

6, Delete data

//db.collectionsNames.remove( { "borough": "Manhattan" } ) 
db.users.remove({age: 12});

By default, remove() deletes all matching data. You need to be careful when using it. If you point to delete a matching data, you need to use justOne

db.users.remove( {age: 12}, { justOne: true } )

Keywords: Database MySQL MongoDB

Added by niranjnn01 on Sat, 05 Feb 2022 20:48:52 +0200