MongoDB common operation command daqo

  • 2020-05-10 23:07:04
  • OfStack

For example, if you want to create an "myTest" database, run the use myTest command first, and then do something (e.g., db.createCollection ('user')) to create a database called "myTest".

1. Common database commands
1, Help view the command prompt

help
db.help();
db.yourColl.help();
db.youColl.find().help();
rs.help();

2. Switch/create database
use yourDB;  When creating a 1 A collection of (table) Will automatically create the current database 

3. Query all databases
show dbs;

4. Delete the currently used database
db.dropDatabase();

5. Clone the database from the specified host
db.cloneDatabase( " 127.0.0.1 " );  Clones the data from the database on the specified machine to the current database 

6. Copies the specified database data from the specified machine to a certain database
db.copyDatabase("mydb", "temp", "127.0.0.1"); Will the machine mydb The data is copied to temp In the database 

7. Fix the current database
db.repairDatabase();

8. View the database you are currently using
db.getName();
db; db and getName Method is 1 Sample of the effect, you can query the current use of the database 

9. Display the current db state
db.stats();

10. Current version of db
db.version();

11. View the current db linked machine address
db.getMongo();

2. Collection aggregation
1. Create a collection (table)

db.createCollection( " collName " , {size: 20, capped: 5, max: 100});// Successful creation will be displayed { " ok " :1}
// Determines whether the set is of constant capacity db.collName.isCapped();

2. Get the aggregation with the specified name (table)
db.getCollection("account");

3. Get all the aggregation sets of the current db
db.getCollectionNames();

4. Display the current state of all clustered indexes of db
use yourDB;  When creating a 1 A collection of (table) Will automatically create the current database 
4
3. User-related
1. Add a user
use yourDB;  When creating a 1 A collection of (table) Will automatically create the current database 
5
2. Database authentication and security mode
use yourDB;  When creating a 1 A collection of (table) Will automatically create the current database 
6
3. Display all current users
use yourDB;  When creating a 1 A collection of (table) Will automatically create the current database 
7
4. Delete users
use yourDB;  When creating a 1 A collection of (table) Will automatically create the current database 
8
4. Aggregate aggregate queries
1. Query all records
use yourDB;  When creating a 1 A collection of (table) Will automatically create the current database 
9
By default, 20 records are displayed per page. When the display is not enough, the next page of data can be queried with it iteration command. Note: type it command without ";"
But you can set the size of the data per page, DBQuery.shellBatchSize = 50; That's about 50 records per page.
2. Query the duplicate data of a column in the current aggregation set after removal
db.userInfo.distinct("name");
 Will filter out name The same data in 
 Is equivalent to: select distict name from userInfo;

3. Query the record age = 22
db.userInfo.find({"age": 22});
 Is equivalent to:  select * from userInfo where age = 22;

4. Query age > The record of 22
db.userInfo.find({age: {$gt: 22}});
 Is equivalent to: select * from userInfo where age >22;

5. Query age < The record of 22
db.userInfo.find({age: {$lt: 22}});
 Is equivalent to: select * from userInfo where age <22;

6. Query age > = 25 records
db.userInfo.find({age: {$gte: 25}});
 Is equivalent to: select * from userInfo where age >= 25;

7. Query age < = 25 records
db.userInfo.find({age: {$lte: 25}});

8. Query age > Is equal to 23 and age < = 26
db.userInfo.find({age: {$gte: 23, $lte: 26}});

9. Query the data of mongo contained in name
db.userInfo.find({name: /mongo/});
// The equivalent of %%
[code]select * from userInfo where name like  ' %mongo%';

10. Query name starting with mongo
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like  ' mongo%';

Query the specified columns name, age data
db.userInfo.find({}, {name: 1, age: 1});
 Is equivalent to: select name, age from userInfo;

Of course, name can also be used with true or false. When ture is used, name:1 effect is 1. If false is used, name is excluded and column information other than name is displayed.
12. Query the specified columns name, age data, age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
 Is equivalent to: select name, age from userInfo where age >25;

13. Sort by age
db.dropDatabase();
1
14. Query the data of name = zhangsan, age = 22
db.dropDatabase();
2
15. Query the first five pieces of data
db.dropDatabase();
3
16. Query the data after 10 items
db.dropDatabase();
4
17. Query the data between 5 and 10
db.dropDatabase();
5
Can be used for pagination, limit is pageSize, skip is page number *pageSize
18. or and queries
db.dropDatabase();
6
19. Query the first data
db.dropDatabase();
7
Query the number of records in a result set
db.userInfo.find({age: {$gte: 25}}).count();
 Is equivalent to: select count(*) from userInfo where age >= 20;

Sort by a column
db.userInfo.find({sex: {$exists: true}}).count();
 Is equivalent to: select count(sex) from userInfo;

Index of 5.
1. Create an index
db.userInfo.ensureIndex({name: 1});
db.userInfo.ensureIndex({name: 1, ts: -1});

2. Query all indexes of the currently clustered collection
db.userInfo.getIndexes();

3. View the total index record size
db.cloneDatabase( " 127.0.0.1 " );  Clones the data from the database on the specified machine to the current database 
2
4. Read all the index information of the current collection
db.users.reIndex();

5. Drop the specified index
db.users.dropIndex("name_1");

6. Delete all indexes
db.users.dropIndexes();

6. Modify, add and delete collection data
1, add
db.users.save({name:  ' zhangsan', age: 25, sex: true});

The data column of the added data is not fixed, and shall prevail according to the added data
2, modify,
db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);
 Is equivalent to: update users set name =  ' changeName' where age = 25;
db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);
 Is equivalent to: update users set age = age + 50 where name =  ' Lisi';
db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);
 Is equivalent to: update users set age = age + 50, name =  ' hoho' where name =  ' Lisi';

3, delete,
db.users.remove({age: 132});

4. Query modification and deletion
db.users.findAndModify({
    query: {age: {$gte: 25}}, 
    sort: {age: -1}, 
    update: {$set: {name: 'a2'}, $inc: {age: 2}},
    remove: true
});
db.runCommand({ findandmodify : "users", 
    query: {age: {$gte: 25}}, 
    sort: {age: -1}, 
    update: {$set: {name: 'a2'}, $inc: {age: 2}},
    remove: true
});

One of update or remove is a required parameter; Other parameters are optional.
Parameter               default
query       query filter condition       {}
If multiple documents meet the query filtering criteria, the first object will be selected in the order specified by this parameter, which will be manipulated by       {}
If remove       is true, the selected object will be deleted before returning       N/A
update       1 modifier object
N/A
If new       is true, the modified object is returned instead of the original object. This parameter is ignored during the delete operation.       false
fields       see Retrieving a Subset of Fields (1.5.0+)
All fields
upsert       creates a new object if the query result is empty. The sample (1.5.4 +)
false
7. Statement block operation
1. Simple Hello World
print("Hello World!");

This method calls the print function, and directly writes "Hello World!" The effect is 1;
2. Convert 1 object to json
db.copyDatabase("mydb", "temp", "127.0.0.1"); Will the machine mydb The data is copied to temp In the database 
1
3. Add data in a loop
db.copyDatabase("mydb", "temp", "127.0.0.1"); Will the machine mydb The data is copied to temp In the database 
2
This adds 30 pieces of data to the loop, and you can also omit the parentheses
db.copyDatabase("mydb", "temp", "127.0.0.1"); Will the machine mydb The data is copied to temp In the database 
3
It is also possible to use db.users.find () to view the information on the next page if multiple data are displayed instead of one page.
4. find cursor query
>var cursor = db.users.find();
> while (cursor.hasNext()) { 
    printjson(cursor.next()); 
}

This queries all the users information, which can also be written like this
var cursor = db.users.find();
while (cursor.hasNext()) { printjson(cursor.next); }

The {} sign can also be omitted
5. forEach iteration cycle
db.copyDatabase("mydb", "temp", "127.0.0.1"); Will the machine mydb The data is copied to temp In the database 
6
One function must be passed in forEach to handle the data information for each iteration
6. Treat the find cursor as an array
db.copyDatabase("mydb", "temp", "127.0.0.1"); Will the machine mydb The data is copied to temp In the database 
7
Gets the data with a subscript index of 4
Now that you can treat it as an array, you can get its length: cursor.length (); Or cursor. count ();
That way we can also display the data in a loop
for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]);

7. Convert the find cursor into an array
db.copyDatabase("mydb", "temp", "127.0.0.1"); Will the machine mydb The data is copied to temp In the database 
9
Convert it to an array using the toArray method
8. Customize our own query results
Show only the age < = 28 and displays only the column age
db.repairDatabase();
0
Exclude columns of age
db.repairDatabase();
1
9. forEach transfer function displays information
db.repairDatabase();
2

Other 8.
1. Query the previous error information

db.repairDatabase();
3
2. Clear the error record
db.repairDatabase();
4
View the aggregation collection basic information
1. See help   db yourColl help();
2. Query the number of data bars in the current set   db. yourColl. count();
3. View the size of the data space;
4. Get db db.userInfo.getDB () where the current aggregation is located;
5. Get the current clustered state db.userInfo.stats ();
6. Get the total size of the aggregate set db.userInfo.totalSize ();
7. Size of aggregation storage space
8. Shard version information   db. userInfo. getShardVersion()
9. The aggregation shall be renamed db.userInfo.renameCollection ("users"); Rename userInfo to users
10. Delete the current aggregation db.userInfo.drop ();

show dbs: Display database list  
show collections : displays collections in the current database (similar to tables in a relational database)  
show users : display user  
use <db name> : toggles the current database, this and MS-SQL Inside meaning 1 sample  
db.help() : displays the database operation commands, which contain many commands  
db.foo.help() : displays the set of operation commands, there are also a lot of commands, foo I'm referring to the current database, 1 The name foo The set is not really a command  
db.foo.find() : for the current database foo Collection for data lookup (since there are no conditions, all data will be listed)  
db.foo.find( { a : 1 } ) : for the current database foo The collection is searched, provided the data is present 1 A property named a And, a The value of 1


Related articles: