This is the 28th day of my participation in the August Text Challenge.More challenges in August

1. Connect to the database

Clear the screen

cls
Copy the code

View a list of all databases

show dbs
Copy the code

2. Create, view, and delete a database

Use database, create database

use itying
Copy the code

If you really want the database to succeed, you must insert a piece of data. You cannot insert data directly into a database. You can only insert data into collections.

Insert data into the USER table of the ITying database.

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

Display the current set of data.

show collections
Copy the code

Delete The specified collection drop table

db.COLLECTION_NAME.drop();
eg:
db.user.drop();
Copy the code

Delete the current database

db.dropDatabase();
Copy the code

3. Insert or add data

Data is inserted, and with the data being inserted, the database is created successfully, and the collection is created successfully.

The name of the table.insert({" name ":" zhangsan ", "age" :20});
Copy the code

4. Find the data

Querying all Records

db.user.find();
Copy the code

Equivalent to: select* from user;

Query for duplicate data in a column of the current aggregated collection after the drop

db.user.distinct("name");
Copy the code

Select distict name from user;

Query the records whose age is 22

db.user.find({"age": 22});
Copy the code

Select * from user where age = 22;

Query the records whose age > 22

db.user.find({"age": {$gt: 22}});
Copy the code

Select * from user where age >22;

Query the records whose age is < 22

db.user.find({"age": {$lt: 22}});
Copy the code

Select * from user where age <22;

Query the records whose age is greater than or equal to 25

db.user.find({"age": {$gte: 25}});
Copy the code

Select * from user where age >= 25;

Query the records whose age is <= 25

db.user.find({"age": {$lte: 25}});
Copy the code

Query age >= 23 and age <= 26

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

Data fuzzy queries containing Mongo in query name are used for searching

db.user.find({"name": /mongo/});
Copy the code

%%, select * from user where name like ‘%mongo%’;

Query for names that begin with mongo

db.user.find({"name": /^mongo/});
Copy the code

Select * from user where name like ‘mongo%’;

Query for names ending in mongo

db.user.find({"name": /mongo$/});
Copy the code

Query the name and age data of specified columns

db.user.find({}, {name: 1, age: 1});
Copy the code

Equivalent to: select name, age from user;

Name can also be true or false, which is the same as name:1.

If false is used, name is excluded and column information other than name is displayed.

Query data in the specified column name and age where age > 25

db.user.find({"age": {$gt: 25}}, {name: 1, age: 1});
Copy the code

Select name, age from user where age >25;

Sort by age 1 ascending -1 descending ascending

db.user.find().sort({"age": 1});
Copy the code

Descending order

db.user.find().sort({"age": - 1});
Copy the code

Example Query data whose name = zhangsan, age = 22

db.user.find({"name": 'zhangsan', "age": 22});
Copy the code

Select * from user where name = ‘zhangsan’ and age = ’22’;

Query the first five items of data

db.user.find().limit(5);
Copy the code

Equivalent to: selectTop 5 * from user;

Query the data after 10 entries

db.user.find().skip(10);
Copy the code

Query data between 5 and 10

db.user.find().limit(10).skip(5);
Copy the code

Can be used for paging, limit is pageSize, skip is (page-1) * pageSize

The or or query

db.user.find({$or: [{"age": 22}, {"age": 25}]});
Copy the code

Select * from user where age = 22 or age = 25;

FindOne Queries the first data

db.user.findOne();
Copy the code

Equivalent to: selectTop 1 * from user; db.user.find().limit(1);

Query the number of records in a result set

db.user.find({"age": {$gte: 25}}).count(a);Copy the code

Select count(*) from user where age >= 20; To return the number of records after the limit, use count(true) or count(non-0) db.users.find().skip(10).limit(5).count(true);

4. Modify data

Modify there are query conditions. Who do you want? Tell Mongo.

Select * from xiao Ming where age = 16;

Db. Student. Update ({" name ":" xiao Ming "}, {$set:{"age":16}});
Copy the code

Age = 33 gender = female

db.student.update({"score.shuxue":70}, {$set:{"age":33And "sex", "female"}});Copy the code

Change all matching items

By default, the update () method updates a single document. To update multiple documents, use the multi option in the update () method.

Db. Student. Update ({" sex ":" male "}, {$set:{"age":33}},{multi: true});
Copy the code

$inc modifier

For example, if we create an online user status record, each change will increase the value specified by $inc on the original basis. If the key is not in the “document”, the key will be created

db.users.update({"name": 'Lisi'}, {$inc: {"age": 50}}, false.true);
Copy the code

Update users set age = age + 50 WHERE name = ‘Lisi’;

db.users.update({"name": 'Lisi'}, {$inc: {"age": 50}, $set: {"name": 'hoho'}}, false.true);
Copy the code

Update users set age = age + 50, name = ‘hoho’ where name = ‘Lisi’;

Note: without the $set keyword, the entire data will be replaced

Db. Student. Update ({" name ":" xiao Ming "}, {" name ":" Ming ", "age" :16});
Copy the code

5. Delete data

Deletes the specified data

db.collectionsNames.remove( { "key": "val" } ) 
eg:
db.users.remove({"age": {$gt:30}});
Copy the code

By default, the remove () method removes all documents that meet the remove criteria. Use the justOne option to limit the delete operation to only one matching document.

db.users.remove( { "age": {$gt:30}}, { justOne: true})Copy the code