On the addition, deletion and query of mongodb database

Time:2021-4-6

View a list of all databases

show dbs

Using database, creating database

use student

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

You can’t insert data directly into a database, you can only insert data into collections. You don’t need to create a collection. You just need to write some syntax to insert data to create a collection

db.student.insert({“name”:”xiaoming”});

db.student The system finds that student is a strange collection name, so it creates the collection automatically. Display the current data set (called table in MySQL)

show collections

Delete database, delete the current database

db.dropDatabase();

Delete collection

db.COLLECTION_NAME.drop()
db.user.drop()

DB. Table name. Insert ({“name”: “Zhangsan”}); student set name (table)

query

1. Query all records

db.userInfo.find();

amount to:
select* from userInfo;

2. Query the duplicate data of a column in the removed current aggregation set

db.userInfo.distinct(“name”);

Will filter out the same data in name
Equivalent to: select distinct name from userinfo;

3. Query the records with age = 22

db.userInfo.find({“age”: 22});

Equivalent to: select * from userinfo where age = 22;

4. Query the records with age > 22

db.userInfo.find({age: {$gt: 22}});

Equivalent to: select * from userinfo where age > 22;

5. Query records with age < 22

db.userInfo.find({age: {$lt: 22}});

Equivalent to: select * from userinfo where age < 22;

6. Query records with age > = 25

db.userInfo.find({age: {$gte: 25}});

Equivalent to: select * from userinfo where age > = 25;

7. Query records with age < = 25

db.userInfo.find({age: {$lte: 25}});

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

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

9. Query name contains Mongo data fuzzy query for search

db.userInfo.find({name: /mongo/});

Equivalent to: select * from userinfo where name like ‘% Mongo%’;

11. Query the specified column name and age data

db.userInfo.find({}, {name: 1, age: 1});

Equivalent to: select name, age from userinfo;
Of course, name can also use true or false, when using ture name:1 The effect is the same. If you use false, you will exclude name and display column information other than name.

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

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

Equivalent to: select name, age from userinfo where age > 25;

13. Sort by age 1 ascending – 1 descending

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

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

db.userInfo.find({name: ‘zhangsan’, age: 22});

Equivalent to: select * from userinfo where name =’zhangsan ‘and age =’22’;

15. Query the first five data

db.userInfo.find().limit(5);

Equivalent to: selecttop 5 * from userinfo;

16. Query the data after 10

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

Equivalent to: Select from userInfo where id not in ( selecttop 10 from userInfo)

17. Query data between 5 and 10

db.userInfo.find().limit(10).skip(5);

Can be used for paging, limit is PageSize, skip is Page * PageSize

18. Or and query

db.userInfo.find({$or: [{age: 22}, {age: 25}]});

Equivalent to: select * from userinfo where age = 22 or age = 25;

19. Findone queries the first data

db.userInfo.findOne();

Equivalent to: selecttop 1 * from userinfo; db.userInfo.find ().limit(1);

20. Query the number of records in a result set and count the number of records

db.userInfo.find({age: {$gte: 25}}).count();

Equivalent to: select count (*) from userinfo where age > = 20;

If you want to return the number of records after the limit, use count (true) or count (not 0) db.users.find ().skip(10).limit(5).count(true)

modify

There are also query conditions in the modification. Who do you want? Tell Mongo.
Look for Xiao Ming and change his age to 16

db.student.update ({“name”: “Xiaoming”}, {$set: {“age”: 16});

Find the math score is 70, change the age to 33:

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

Change all matches:

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

Complete replacement, do not appear $set keyword: note

db.student.update ({“name”: “Xiaoming”}, {“name”: “Daming”, “age”: 16});

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’;

delete

db.collectionsNames.remove( { “borough”: “Manhattan” } )
db.users.remove({age: 132});

Delete one

db.restaurants.remove( { “borough”: “Queens” }, { justOne: true } )

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]