node. js 0 Basic Detailed tutorial (6): MongoDB Database operations

Source: Internet
Author: User
Tags modifier install node

The sixth chapter recommends 4 hours of study time 10 chapters

How to learn: read it in detail and implement the relevant code manually

Learning Goals : This tutorial will teach you to install node, build servers, Express, MySQL, MongoDB, write background business logic, write interfaces, and finally complete a complete project backstage, a total of 10 days is expected.

Database

Database management structure, generally divided into two kinds: b/S architecture c/s structure.

b/S architecture: Browser/server, Browser/server mode, that is, through the browser and server-side database to interact

C/S Architecture: Client/server, client/server mode, that is, through the client and server database interaction (we use the cmd command line can be considered a client)

Database is divided into: relational database and non-relational database according to data structure

A relational database is like a generally regular form that allows you to locate a piece of data based on a horizontal portrait, such as

General databases are relational databases, such as Mysql,oracle

A non-relational database is not able to locate a piece of data horizontally and vertically, and its structure is more like a bunch of JSON data in an array, such as

The MongoDB we're about to learn is a non-relational database.

The current database of fire comparisons is: Oracle, MySQL, SQL Server

Compared to these databases, MongoDB has a small size, convenient and fast operation, very suitable for the rapid development of small and medium-sized projects

And it's a good configuration for node. js to use (node. JS can also manipulate other databases, and there will be time to give you a separate introduction to node. JS Operation MySQL)

MongoDB Installation

1. Download the required installation software on the official website

:https://www.mongodb.com/download-center#community

Download the appropriate version of the MongoDB installation package as required

After you double-click the installation package, click Run

Go to the MongoDB installation page and Click next to proceed to the next step

After you choose Agree, click next to proceed to the next step

Changing the installation directory is not recommended here, just click Complete to proceed to the next step

Click Install for installation

Wait for the installation progress bar to complete

After the installation progress bar completes, it will go to the completion page, Click Finish to complete the MongoDB database installation

Complete the steps, the installation is successful

2. detect if MOngodb is successfully installed and configure the start command

inthe bin directory of the installation directory of M Ongodb , open the cmd command line, enter the command ' mongod–help ' appears now this installation succeeded

Set storage location and configuration of data and logs MongoDB service

(1) Set the data file and log file storage location

Set the storage location of the file according to your hobby, the storage location of this installation document is set on the E drive, the directory is as follows

E:\MongoDaTa

├─data #MongoDB数据库中的数据的储存位置

└─log #MongoDB数据库中的日志的储存位置

└─mongodb.log # Storage files for logs in the MongoDB database

(2) Configuring the MOngodb service

1/ setting the environment variables for MongoDB

Computer > right-click > Properties > Advanced system Settings > Environment Variables > Find the PATH variable in the system variable

Add the address of the bin directory at the end of the path variable to the MongoDB installation directory

If you install the MongoDB installation directory under this document, the Bin directory is:

C:\Program Files\mongodb\server\3.4\bin

After setting the settings, click OK, then set up the MONGODB environment variable.

2/ Install MongoDB Service

First step: Modify the following command according to the storage location of the settings

(PS: If the storage location is set by this document, do not change it)

Mongod--logpath "E:\MongoData\log\mongodb.log"--logappend--dbpath "E:\MongoData\data"--directoryperdb-- ServiceName "MongoDB"--servicedisplayname "MongoDB"--install

#命令中字段的说明

--logpath Specify the storage path for the MongoDB log file, note that the specified file is not a directory

--logappend writing the log using the Append method

--dbpath specifying the storage path for database data

--directoryperdb A new directory per db

--servicename Specifying the service name

--servicedisplayname Specifies the service name, which is executed when there are multiple mongodb services.

--install is specified as a Windows service installation.

The second step: open the cmd command line at the beginning with the administrator, enter the first step of the above modified instructions return (PS: Be sure to run as an administrator), (run complete without any error prompts, indicating that the setting is successful)

Step three: Start the MongoDB service

Net Start MongoDB

Fourth step: Close MongoDB Service

Net Stop MongoDB

MongoDB operations

Run cmd as administrator, then execute MONGO, enter MongoDB's command state

Show DBS shows the current database, the first two are self-brought, and the next two are created by myself (everyone only has two databases in front of it)

Use data name to switch databases or create a new database, I created a database named Demo3.

Db.createcollection ("table name") adds a table to the database, and a table is created below T1

DB Displays the database that is currently in use

Show tables displays the table under the database

Create a table again T2

Db.collection.drop () to delete a table from the database

The command deletes the T1 table, and returns true to indicate that the deletion was successful and that the table was T1 when it was displayed.

Db.dropdatabase () Delete the database that is currently in use

When Demo3 is removed and the database is displayed, there is no DEMO3.

We re-create the DEMO3 database, insert the T1 table, and then insert the data

Insert Data db. Table name   . Insert ({}); parameter , the system automatically adds a unique _id to the data when the insert is successful

use db. Table name . Find () View Data

Query can take parameters, the JSON can be passed in the query. Here we add two data (note: The added data name is Zhangsan1, zhangsan2, insert different values for easy use later), and then implement the query with parameters and without parameters, the results are different

Modify Data Update ({modified condition },{ modifier:{ modified }}, third argument, fourth parameter )

modifier : $set Modify $unset Delete a field $inc when in the programming language the " += "

The third parameter means that if there is no record of update, insert objnew,true is inserted , default is false, do not insert.

A fourth parameter, The default is false, update only the first record found, if this parameter is true, the condition is checked out all the records are updated.

Let's change a piece of data, the modifier uses the $set

After running the following code, we find that the age in the data of name Zhangsan is modified to 50 (originally 25)

Delete the data field, using the $unset

The following code removes the Age property of the data named Zhangsan (the property value after age is arbitrarily written, because it is deleted, and it doesn't make sense to write much, but it needs to write a JSON format)

+ = One piece of data, using $inc

The following code overlays 10 on the age of the data named Zhangsan2 (originally 25, which becomes 35 after execution)

Mongo supports the use of JS code, including declaration variables, loops, etc.

The following loop inserts the ten data. (Although the hint ninserted:1 , but has already inserted the ten data)

You can see the 10 data in the query.

Query JSON supports advanced selection methods

$lt less than <

$GT greater than >

$gte greater than or equal to >=

$lte less than or equal to <=

Such as:

Db.t1.find ({age:{$lt: 5}}) to find all data of age less than 5

Db.t1.find ({age:{$lt: 5, $GT: 1}}) to find all data with age less than 5 greater than 1 (multiple filter criteria separated by commas)

The basic operation is to say so much, I hope you put all the above commands are finished.

Tomorrow we will explain: node. js operation MongoDB, and how to package

node. js 0 Basic Detailed tutorial (6): MongoDB Database operations

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.