MongoDB usage (use in node)

Source: Internet
Author: User
Tags clear screen mongodb mongodb driver mongodb save mongodb server
Database Concepts

Databases: Warehouses for data storage

Database is a convenient management of data for us a platform, such as data storage, modification, query and so are very convenient. Database Classification

There are a lot of database products, and here are some common database products: MySQL Oracle DB2 SQL Server MongoDB etc.

The database does not have the rank cent, each has each application scene, we study here is the MongoDB database. MongoDB Database

In order to better learn MongoDB database, you can refer to the Novice tutorial on the MongoDB Database tutorial documents,
link address: http://www.runoob.com/mongodb/mongodb-tutorial.html

MongoDB Database official website: https://www.mongodb.com/

Download Address: Https://www.mongodb.com/download-center?jmp=nav
Choose a good version of the download can be
Why Choose MongoDB?
Reason:

    1. As long as will be JS will MongoDB (learning cost Low)
    2.mongodb to JS compatibility best
    3.MONGODB save data is save JSON
Installing and configuring the MongoDB database environment
See the video:

How to verify the success
    of the installation: in the CMD terminal input: MONGO--version or Mongod--version, can see the version is good, indicating the installation success

if the terminal input MONGO--version Hint cannot find the command, be sure to see if there is no configuration MongoDB environment variables, and configure the environment variables must remember to restart CMD
problems existing in Window7 installation MongoDB
Recommendation: Upgrade to WINDOW10

solution steps:
    1. Upgrade your operating system to WINDOW7 latest version
    2. Install Vc_redist.x64.exe 
    3. If your computer is 32, Then install the 32-bit MongoDB software Mongodb-win32-i386-3.2.11-signed.msi
MongoDB Database storage structure

Compare with Excel to understand:


core idea:
    1. The categorized storage data
    2.mongodb is a ' set ' concept to distinguish between different data, such as user collection, collection of goods, order collection, etc. Different data to be placed in different sets of unified management
    3. The collection can be thought of as an array of
    4. Each object in the array can be thought of as a JSON object, which we call ' document ' 5 in MongoDB
    . The structure of the document can be arbitrary, But must be unified, that is, in the same set, all documents must adhere to the same data structure

other:
    ' Set ' function is to isolate data, classify the storage of data
steps to manipulate MongoDB
1. Create a storage directory for the data in the Database
2. Start MongoDB Service Instance
3. Create a database
4. Create a collection
5. Create a document
MongoDB the required steps before using 1--create the Data directory (required)
We need to create a directory in which to store the data after the installation completes MongoDB. Note that the data directory should be placed under the root directory (such as: C:\ or D:\. And so on), the name of the data directory casually, such as I named Mongodb_datas
MongoDB must be done before using 2--to start the server (required)
The way to start a MongoDB server: Start with a terminal command 1. First switch to the bin directory of your MongoDB installation directory (if you have configured the environment variable in which directory it is possible) 2. Execute at Terminal: Mongod.exe--dbpath C : \mongodb_datas, if it's successful, it means everything's OK. Note: The 1.32-bit version uses the following command to start the data service: ' Mongod--dbpath data store path--jou Rnal--storageengine=mmapv1 ' 2. After executing the above command and successfully opening the MongoDB data Service instance, the console can be minimized to one side, do not close, otherwise can not connect,

If the operation of the database is complete, you can turn the console off by ' Ctrl + C '. How to start a MongoDB server two: the continuation of mode one, the above instructions into a batch file. Bat starts the MongoDB server three: To run the MongoDB server as a Windows service please note that you must have administrative privileges to run the following command.  Run the MongoDB server as a Windows service by executing the following command: Mongod.exe--bind_ip youripadress--logpath "C:\data\dbConf\mongodb.log"--logappend --dbpath "C:\data\db"--port yourportnumber--servicename "Yourservicename"--servicedisplayname "YourServiceName" Install Example: Mongod.exe--bind_ip 127.0.0.1--logpath "C:\mongodb_datas\server_log\mongodb.log"--logappend-- DBPath "C:\mongodb_datas"--port 27017--servicename "Mongodbserver"--servicedisplayname "Mongodbserverdisplayname"- -Install Add: Uninstall MongoDB service from Windows Service 1. First stop the service you want to delete (for example, Mongodbserver) 2. Run cmd as administrator, enter in cmd: SC Delete Service name
 Say (for example, mongodbserver) Note: 1.mongodb.log This log file must first be created in order to execute instruction 2. You must open the CMD window as an administrator and run the
How to connect to the MongoDB server and manipulate it.

Note: Make sure your service instance is turned on before making the connection (do not close the data service instance that you just opened).

There are two ways to connect to a MONGODB server and manipulate it to connect Mode 1: Use MongoDB's own MongoDB admin Shell to enter in the terminal MONGO or MONGO--host 127.0.0.1,--port 27017  The MONGO command defaults to connecting the MongoDB service instance on this computer: ' 127.0.0.1:27017 ', which specifies the host name and port number of the connection through the following command: ' MONGO--host 127.0.0.1 27017 ' If you see a word similar to the following: ' MongoDB shell version v3.4.2 connecting to:mongodb://127.0.0.1:27 017 MongoDB Server version:3.4.2 server has startup warnings:2017-01-18t18:49:53.865+0800 I control [Initan Dlisten] 2017-01-18t18:49:53.865+0800 I control [Initandlisten] * * warning:access control isn't enabled for the DAT
    Abase. 2017-01-18t18:49:53.866+0800 I control [Initandlisten] * * * Read and Write access to data and configuration are UNR
    Estricted.

2017-01-18t18:49:53.866+0800 I control [Initandlisten] > ' If you are prompted to "Cannot connect to host", check that your MongoDB data service instance is turned on. Operation 1: In the terminal using MONGODB instructions to operate common instructions: see next knowledge point Connection and operation mode 2: Using Graphical Client tools: Robomongo (cross-platform): Https://robomong O.Org/download
 
Basic Operations Command
can refer to:
    http://www.yiibai.com/mongodb/mongodb_create_collection.html

    http://www.runoob.com/mongodb/ Mongodb-tutorial.html
Show DBS View all database use database names on the current service instance if the database does not exist, create the database, or switch to the specified database. DB view current database Show collections view all collections in the current Database Db.createcollection ("collection name") to create a collection of DB. Collection name. Insert ({data document}) Each of the inserted documents will automatically help us generate a _id field that is MongoDB automatically maintained and does not require us to care about DB. Collection name. Find () query specifies that all data in the collection can be passed through DB. Collection name. Find (). Pretty () to beautify the output format The default is to query all, through: DB. Collection name. Find ({query condition}) queries the data db in the collection by criteria. Update ({update condition}, {field to update}) updates the specified collection data, note that the field to be updated must be written like this {$set: { Name of field: field value}, db. Collection name. Remove ({delete condition}) Delete data in the specified collection exit the current operation

CLS Clear Screen

Note: Updates and deletions generally require conditions, except for all updates and deletions, but all updates and deletions are dangerous, and the Node operations are rarely used during the actual operation MongoDB

Install the official MongoDB driver package:

NPM Install--save MongoDB

Please refer to the official documentation for the specific operation: Https://www.npmjs.com/package/mongodb

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.