Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

preface

MonogoDB differs from MySQL,Redis and other databases in that it is based on distributed file storage. Written in C++, it is designed to provide scalable high-performance data storage solutions for WEB applications, so it is the closest database to the front end. Its entire document storage is stored in the document in JSON format. MongoDB is a product between relational database and non-relational database, non-relational database has the most rich function, the most like relational database. In this article, we will talk about the installation and basic use of MongoDB.

Installation and environment variable configuration

download

MonogoDB download address

The installation

After downloading, follow the instructions and install foolproof.

The default installation location is:C: \ Program Files \ mongo \ Server \ 4.4

Configuring environment Variables

If you enter a command on the terminalmongod --versionIf no error is reported, environment variables need not be configured. Otherwise, environment variables need to be configured.

  1. Open your desktop, right-click your computer, and select Properties
  2. Select Advanced System Settings
  3. Click environment variable and configure the Path under system variable Path:C: \ Program Files \ mongo \ Server \ 4.4 \ bin

At this point, mongoDB is installed.

Configuration mongo

Mongo 4.0 automatically generates mongod. CFG configuration files, data and log folders

Open the mongod. CFG

Example Change the dbPath path

DbPath: C: \ Program Files \ mongo \ Server \ \ data \ 4.4 db

Example Change the systemLog path

Path: C: \ Program Files \ mongo \ Server \ 4.4 \ log \ mongod log

Start the Mongod service

Mongod -- congig "C: \ Program Files \ mongo \ Server \ 4.4 \ bin \ mongod CFG"

If no error is reported and the program is not finished, the startup is successful. Enter the information in the browserlocalhost:27017Returns a string indicating that the service is accessible.

Install the Mongod service

We have been able to successfully start the Mongod service, but if the DOS interface is closed, the Mongod service will stop, we want the Mongod service to always open, can execute the following command, install the service! After that, as long as the computer is turned on, it will automatically start, very nice!

Mongod --congig "C: Program Files\MongoDB\Server\4.4\bin\mongod. CFG "--serviceName "MongoDB" --install

4. MongoDB syntax

Comparison between SQL and MongoDB

SQL MongoDB
Table (Table) Collection (collection)
Row (Row) The Document (the Document)
Column (Col) Field (Filed)
The Primary Key (Primary Key) Object ID (ObjctId)

Database operations

operation The command
Creating a database use demo
Viewing a Database show dbs
Deleting a Database dp.dropDatabase()
> use test
switched to db test
> show dbs
admin   0.000GB
config  0.000GB
local0.000 GBtest0.000 GB > db.dropDatabase()
{ "dropped" : "test"."ok": 1} > show DBS admin 0.000GB config 0.000GBlocal0.000 GBCopy the code

Set operations

operation The command
Create a collection db.createCollection(name)
See the collection show collections
Delete the collection dp.collection.drop()
> use test
switched to db test
> db.createCollection("article")
{ "ok" : 1 }
> show collections
article
> db.article.drop()
true
Copy the code

The document operation

operation The command
Create a document db.ccollection.insert({})
To view the document show collection.find({})
Delete the document dp.collection.remove()
Update the document dp.collection.update({},{},fasle,true)

The third parameter of the update document operation is false, indicating whether to insert the new document was not found, and the fourth parameter indicates whether to batch update the document. False indicates that only the first matched document is updated.

> db.article.insert({"name":"jack"})
WriteResult({ "nInserted" : 1 })
> db.article.insert({"name":"Bob"})
WriteResult({ "nInserted" : 1 })
> db.article.insert({"name":"Steven"})
WriteResult({ "nInserted" : 1 })
> db.article.find({})
{ "_id" : ObjectId("6156da586eb4dff864f4c2de"), "name" : "jack" }
{ "_id" : ObjectId("6156da5f6eb4dff864f4c2df"), "name" : "Bob" }
{ "_id" : ObjectId("6156da726eb4dff864f4c2e0"), "name" : "Steven" }
> db.article.remove({"name":"jack"})
WriteResult({ "nRemoved" : 1 })
> db.article.find({})
{ "_id" : ObjectId("6156da5f6eb4dff864f4c2df"), "name" : "Bob" }
{ "_id" : ObjectId("6156da726eb4dff864f4c2e0"), "name" : "Steven" }
> db.article.update({"name":"Bob"}, {$set: {"name":"Frank"}},false.true)
WriteResult({ "nMatched" : 1, "nUpserted": 0."nModified" : 1 })
> db.article.find({})
{ "_id" : ObjectId("6156da5f6eb4dff864f4c2df"), "name" : "Frank" }
{ "_id" : ObjectId("6156da726eb4dff864f4c2e0"), "name" : "Steven" }
Copy the code

Query operation

operation The command Commands in SQL
Is equal to the {<key>:<value>} Where title = 'MongoDB tutorial '
Less than {<key>:{$lt:<value>}} where likes < 50
Less than or equal to {<key>:{$lte:<value>}} where likes <= 50
Is greater than {<key>:{$gt:<value>}} where likes > 50
Greater than or equal to {<key>:{$gte:<value>}} where likes >= 50
Is not equal to {<key>:{$ne:<value>}} where likes ! = 50
> db.article.remove({"name":"Frank"})
WriteResult({ "nRemoved" : 1 })
> db.article.find({})
{ "_id" : ObjectId("6156da726eb4dff864f4c2e0"), "name" : "Steven" }
>
Copy the code

5. Client operation

Robo3T download address

Compared to the command line operation mode, using the client side operation will be more simple and convenient!

The last

⚽ this article mainly introduces the installation and basic use of MongoDB ~ ⚾ if you are interested in this article welcome to like attention + collection, more wonderful knowledge is waiting for you! 😘 🏀GitHub blogs at github.com/Awu1227. 🏉 I have other columns, please read ~ 🏐 play the beauty of CSS 🎱Vue from giving up to getting started 🎳 simple JavaScript