Build the Environment
Environment for WIN8 x64, download installation omitted. After that, the MongoDB Bin directory is added to Windows's path for later ease of use.
C-Disk New storage folder: c:/data/db
Run Service: win+r, enter mongod.exe--dbpath c:/data/db return, do not close this window
Start client: win+r, enter MONGO return
Database Operations
View current database name:DB
View all databases: show dbs or show databases
Switch database: use mydb
Switch database: Usemydb
Data Manipulation
InsertCreate data: j={name: ' MONGO '}
K={x:3}
Inserts the specified collection : Db.testData.insert (j);
Db.testData.insert (k);
See which collections are in the current database: Show Collections
View all data in the specified collection (20 per display, input it shows the next 20): db.testData.find ();
Insert multiple data through JavaScript for loop:
for (Var i=0;i<100;i++) {
Db.testData.insert ({num:i,name: ' name ' +i})
}
MongoDB's JavaScript console can be directly input to JavaScript code execution, similar to the Nodejs console.
EnquiryQuery pointerswhen querying a collection, MONGODB returns a pointer object containing the query result set
var c = Db.testData.find ();
Traverse the Pointer object:
while (C.hasnext ()) Printjson (C.next ());
Using pointer arrays
Use the pointer object directly as an array
Printjson (C[4]);
When the pointer content is accessed using the above method, the MONGO implementation calls the Cursor.toarray () method, loads the data returned by the pointer from memory, and then accesses it as an array.
This operation will completely traverse the entire pointer, and for very large result sets, memory overflow may occur.
Conditional queryquerying records for a specified field
Db.testData.find ({num:5});
Query a single record (if there is more than one, return to the first article)
Db.testData.findOne ({num:5});
Specifies that the result set contains the number of record bars
Db.testData.find (). Limit (5);
Getting Started with MongoDB CRUD basics