First, the common command
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 created automatically when a collection (table) is created
3, query all databases
4, delete the current use of the database
5, clone the database from the designated host
Clones data from the database on the specified machine to the current database
6. Copy the specified database data to a database from the specified machine
Db.copydatabase ("MyDB", "temp", "127.0.0.1");
Copy the MyDB data from this computer to the TEMP database
7, repair the current database
8. View the database currently in use
db DB and GetName methods are the same effect, you can query the currently used database
9, display the current DB status
10, the current DB version
11, view the current DB link machine address
Second, Collection (table) aggregation set
1. Create a Clustered collection (table)
Db.createcollection ("Collname", {size:20, Capped:5, max:100});//Create success will show {"OK": 1}
2, get the specified name of the aggregation set (table)
Db.getcollection ("account");
3, get the current db of all the aggregation set
Db.getcollectionnames ();
4. Displays the status of all clustered indexes in the current DB
Db.printcollectionstats ();
Third, user-related
1. Add a user
Db.adduser ("name");
Db.adduser ("UserName", "pwd123", true);
Add user, set password, read only
2. Show all current users
3, delete the user
Db.removeuser ("UserName");
These are some of the most basic commands that I took as notes. Deeper crud I haven't even tried until I try to write again.
Statement block operations
1, simple Hello World
This type of writing invokes the print function, and writes directly to "Hello world!" The effect is the same;
2. Convert an object to JSON
Tojson (New Object ());
Tojson (New Object (' a '));
3. Recycle Add Data
for (var i = 0; i < i++) {
... db.users.save ({name: "u_" + I, age:22 + i, sex:i% 2});
... };
This allows you to add 30 of data to the loop, but also omit the notation of parentheses.
for (var i = 0; i < i++) Db.users.save ({name: "u_" + I, age:22 + i, sex:i% 2});
is also possible, when you use Db.users.find () query, display more than one page of data, you can use it to view the next page of information;
4, Find cursor query
var cursor = Db.users.find ();
while (Cursor.hasnext ()) {
Printjson (Cursor.next ());
}
This allows you to query all the users information, as well as write
var cursor = Db.users.find ();
while (Cursor.hasnext ()) {Printjson (cursor.next);}
You can also omit the {} number
5. Foreach Iterative cycle
Db.users.find (). ForEach (Printjson);
foreach must pass a function to handle the data information for each iteration
6, the Find cursor when the array processing
var cursor = Db.users.find ();
CURSOR[4];
Get the data for the subscript index of 4
Since it can be treated as an array, it can be obtained in length: Cursor.length () or cursor.count ();
So we can also use the loop to display the data.
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 by using the ToArray method
8. Customize our own query results
Show only age <= 28 and show only age this column of data
Db.users.find ({age: {$lte:}}, {age:1}). ForEach (Printjson);
Db.users.find ({age: {$lte:}}, {age:true}). ForEach (Printjson);
Exclude an Age column
Db.users.find ({age: {$lte:}}, {Age:false}). ForEach (Printjson);
9, the foreach transfer function display information
Db.things.find ({x:4}). ForEach (function (x) {print (Tojson (x));});
Other
1, the error message before the query
2. Clear Error Record
3. Display Database list
4, display the collection in the current database (similar to tables in the relational database)
5. Display user
6, switch the current database, this and ms-sql inside the same meaning
use