MongoDB's mongo shell common operation methods and operation script notes
- 2020-06-12 10:53:37
- OfStack
1. Common commands
1, Help view command prompt
help
db.help();
db.yourColl.help();
db.youColl.find().help();
rs.help();
2. Switch/create database
use yourDB; The current database is automatically created when a collection (table) is created
3. Query all databases
show dbs;
4. Delete the database currently in use
db.dropDatabase();
5, clone database from specified host
db.cloneDatabase( " 127.0.0.1 " );
Clones 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 native mydb data to the temp database
7. Fix the current database
db.repairDatabase();
8. View the database you are currently using
db.getName();
db; The db and getName methods are one effect of querying the currently used database
9. Display the current db status
db.stats();
10. Current VERSION of db
db.version();
11. View the machine address of the current db link
db.getMongo();
2. Collection(table) aggregate collection
1. Create an aggregation collection (table)
show dbs;
0
2. Get the aggregation set with the specified name (table)
db.getCollection("account");
3. Obtain all the aggregate sets of the current db
db.getCollectionNames();
4. Display the current status of all db clustered indexes
show dbs;
3
3. User relevance
1. Add 1 user
show dbs;
4
Add user, set password, read-only
2. Display all current users
show dbs;
5
3. Delete users
show dbs;
6
These are some of the most basic commands that I'll use as notes. I haven't even tried crud, but I'll wait until I do.
Statement block operation
1. Simple Hello World
print("Hello World!");
This method calls the print function, and directly writes "Hello World!" The effect of theta is 1;
2. Convert 1 object to json
tojson(new Object());
tojson(new Object('a'));
3, loop to add data
show dbs;
9
This adds 30 pieces of data to the loop, and you can also omit the parenthesis
for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
It is also possible. When you use ES108en. users. find() to query, you can use it to view the information on the next page if multiple data are displayed and one page cannot be displayed.
4. find cursor query
var cursor = db.users.find();
while (cursor.hasNext()) {
printjson(cursor.next());
}
This queries all users information and can be written as follows
var cursor = db.users.find();
while (cursor.hasNext()) { printjson(cursor.next); }
You can also omit the {} sign
5. forEach iteration cycle
db.users.find().forEach(printjson);
One function must be passed in forEach to process the data information for each iteration
6. Treat find cursor as an array
var cursor = db.users.find();
cursor[4];
Gets the data with a subscript index of 4
Since 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 find cursor to array
var arr = db.users.find().toArray();
printjson(arr[2]);
Convert it to an array using the toArray method
8. Customize our own query results
Show only the age < = 28 and displays only the age column
db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson);
db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);
Exclude age columns
db.users.find({age: {$lte: 28}}, {age: false}).forEach(printjson);
9, forEach transfer function display information
db.things.find({x:4}).forEach(function(x) {print(tojson(x));});
other
1, query the previous error information
db.getPrevError();
2. Clear the error record
db.resetError();
3. Display database list
show dbs
4. Display collections in current database (similar to tables in relational database)
show collections
5. Display users
show users
6. Switch the current database, which is the same as es186EN-SQL
use