Getting Started with MongoDB CRUD basics

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.