Mongodb learning (Building a Development Environment + getting started) and mongodb Building

Source: Internet
Author: User
Tags install mongodb mongodb client

Mongodb learning (Building a Development Environment + getting started) and mongodb Building

In the next period, I will learn about mongodb from a simple perspective and record the learning process in detail. What is mongodb? I don't need to explain this too much. Okay. Let's go straight to the theme and see how to build the mongodb development environment. Note: During my learning of mongodb, all my operations were completed in windows. Next I will take you step by step to learn how to build the mongodb development environment in windows.

Download mongodb

First, we need to download mongodb from the official mongodb website.
Http://www.mongodb.org/downloads

Create a folder mongodb

I created a folder named mongodb under drive D, which will be used later to store the mongodb Installation File. Decompress the downloaded mongodb and copy all the decompressed files to the folder. :

Create a folder named mongodbData

I create a new mongodbData folder under drive D. This folder is used to store mongodb data, including set data (that is, the corresponding table data in mysql) and log files.

How to install mongodb

We can find that there is a bin directory after the mongodb file is decompressed. There are a lot of exe files under this directory. First, we enter this directory under the command line and execute the following command:
Mongod.exe-dbpath D: \ mongodbData

Here, the "-dbpath D: \ mongodbData" parameter indicates the path where the mongodb data file is stored. Here, I wrote the newly created directory and press Enter. The interface shown in is displayed:

At last, we can see that the default port of mongodb is 27017.
The following file is displayed in the mongodbData folder on drive D:

So far, mongodb has been installed, but you still need to configure environment variables.

Configure Environment Variables

Configure the bin directory of mongodb after decompression to the path environment variable. If you have configured jdk environment variables, you should know how to operate them. I will not describe it here ,:

Enter "cmdd-help" in the cmd command line to display the following interface, indicating that the environment variable is successfully configured.

So far, our mongodb has been successfully installed and configured, and the data storage location has been specified. How should we start the mongodb database ??

Start mongodb Database

Enter the cmd command line and enter mongod-dbpath D: \ mongodbData. Note that there are two "-"

We can see that our mongodb service has been enabled and listens to the default port 27017. Note that if we access the service through web, we need to add Port 1000, that is, port 28017.

Mongodb Quick Start

Let's first compare the storage differences between mongodb and mysql.

Currently, the json data format 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 format of the stored data is exactly the same as that of json, Which is saved in the form of "key: value". We call it "Gson" and add many data types to Gson. In mongodb, each document (that is, a row of data in mysql) can contain different data, which is why mongodb is highly scalable.

Create a mongodb Client

Before formally creating a database, we need to create a mongodb client.
It is also easy to create a client. You only need to enter "mongo 127.0.0.1: 27017" in the cmd command line. Note that before starting the client, you must run the "mongod-dbpath D: \ mongodbData "starts the mongodb Service

We can see that the system uses the "test" user by default. If we need to switch to the "admin" Super administrator, We can enter this command. I will open a new cmd window and enter "mongo 127.0.0.1: 27017/admin"

Create a database and add data

To create a database in mongodb, you only need to enter the "use Database Name". However, if we do not add any data to the database, when we close the client, the database will disappear. Run the following commands in sequence:
Use mydb// Create a database. If no data is added at this time, the database will be deleted if you exit the client.
Db. students. insert ({name: "zhangsan "})// Db indicates mydb, and students indicates a set (corresponding to the table in mysql). We add a document key = "name", value = "zhangsan" to the students set.
Show dbs// View the databases in the current system
Show collections// Query which sets are available
Db. students. find ()// View the data in the Set
Db. students. findOne ()// View the first data in the Set

As you can see, I added one or two pieces of data to the students set. The system automatically added a key called "_ id" to me.

How can I change the data ?? For example:

The modified statements are in the same red border. Here, the modifier "$ set" is used. If the modifier is not used, the existing data will be overwritten. This is obviously unreasonable. You can see that I changed the document name = "zhangsan" to name = "lisi", but the careful programmer may find that when I query it again, I only modified the first data, this will be detailed in my subsequent blog.

What should I do if I need to delete data ??? For example, if you want to delete data with name = "zhangsan", you can write:
Db. persons. remove ({name: "zhangsan "})

Now, let's get started with building a development environment and getting started with mongodb today. I hope you will enjoy it.

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.