MongoDB Learning (Build a development environment + simple Introduction)

Source: Internet
Author: User
Tags install mongodb mongodb client

For the rest of the day, I will learn about MongoDB, and I will take a detailed note of the learning process. What is MongoDB? I don't think I need to explain that much. Okay, nonsense, let's go straight to the topic and see how the MongoDB development environment is built. Note that all of my operations are done under Windows during the learning of MongoDB. Next I will take you step after day to learn the construction of the Windows MongoDB development environment

Download MongoDB

First we need to go to MongoDB's official website to download MongoDB.
Http://www.mongodb.org/downloads

Create a new Folder MongoDB

I set up a new folder called MongoDB under D, which is used to store MongoDB's installation files later. Then unzip the MongoDB that we downloaded and copy all the extracted files to the folder below. :

Create a new folder Mongodbdata

I created a new Mongodbdata folder under D, which is used to store mongodb data, including the collection data (that is, the corresponding table data in MySQL), and the log file.

How to install MongoDB

We can find that after the MongoDB file decompression, there will be a bin directory, in this directory there are many EXE files we first enter the directory under the command line, execute the following command:
Mongod.exe–dbpath D:\mongodbData

Explain, here the "–dbpath D:\mongodbData", the parameters in the data file of the MongoDB storage path, here I wrote the new directory, press ENTER at this time. will appear as shown in the interface:

At the end we can see that the default port for MongoDB is 27017
The following file appears under the Mongodbdata folder under the D disk:

At this point, MongoDB is installed, but you also need to configure environment variables.

Configuring Environment variables

To configure the bin directory of the previously extracted MongoDB to the PATH environment variable, the classmate who has configured the JDK environment variable should know how to operate, here I will not say more:

We entered the cmd command line: "Mongod-help" appears as follows, indicating that our environment variable is configured successfully.

So far, our MongoDB has been successfully installed and configured successfully, and also specify the location of the data, then we should start the MongoDB database??

Start the MongoDB database

Go to cmd command line, enter Mongod–dbpath D:\mongodbData note Here are two "–"

As you can see, our MongoDB service is turned on and listening to the default port 27017, note that if we are "web" access, we need to add 1000, or 28017 ports.

MongoDB easy to get started

Let's compare the storage between MongoDB and MySQL.

The JSON data format currently transmitted over the network is very common, in MongoDB, our data storage format is added on the basis of JSON, that is, in MongoDB, the data format and JSON are stored exactly the same, all through the form of "Key:value" to save, We call it "Gson" and add a lot of data types to the Gson. In MongoDB, each document (i.e. a row of data in MySQL) can contain different data, which is why MongoDB has a strong extensibility.

Create a MongoDB client

Before we formally create a database, we need to create a MONGODB client.
Creating the client is also very simple, we only need to enter "MONGO 127.0.0.1:27017" at the cmd command line, note that before starting the client, the MongoDB service must be started with the command "Mongod–dbpath D:\mongodbData"

Can see the system will default to use "test" This user, if we need to switch to "admin" super Administrator, you can enter the command, I reopen a cmd window input "MONGO 127.0.0.1:27017/admin"

Create a database to add data

Creating a database in MongoDB is simple, just enter the "Use database name", but if we don't add any data to the database, the database will disappear when we close the client. I enter the following command in turn:
Use mydb //CREATE DATABASE, if you do not add any data at this time, exit the client, the database will be deleted.
Db.students.insert ({name: "Zhangsan"}) //db means that mydb,students represents the collection (corresponding to the table in MySQL), we add a students "name" to the Key= collection , value= "Zhangsan" document
Show DBS //view those databases in the current system
What collection does the show collections //query system have
db.students.find () //View the data in the collection
Db.students.findOne () //View the first piece of data in the collection

As you can see, I added one or two data to the students collection, and the system automatically adds a key called "_id" to me.

So how do I change the data?? Such as:

The same red border inside is my modified statement, here to use the modifier "$set", if you do not use the modifier, will appear to overwrite the existing data phenomenon, which is obviously unreasonable. You can see that I changed the document Name= "Zhangsan" to Name= "Lisi", but the careful program ape might find that when I query again it just modifies the first piece of data, which I'll explain in detail in a subsequent blog post.

If you need to delete data, what should you do??? For example I need to delete name= "Zhangsan" data, I can write:
Db.persons.remove ({name: "Zhangsan"})

Well, today's MongoDB building development environment + simple introduction, it is here, I hope you can like.

MongoDB Learning (Build a development environment + simple Introduction)

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.