MongoDB installation configuration and introduction

Installation and configuration

1. Download the windows version of MongoDB, there are 32-bit and 64-bit versions, download according to the system situation, download address:
http://www.mongodb.org/downloads

2. Installation , the default installation path is:
C:\Program Files\MongoDB\Server\bin

3. Configure environment variables : add the default installation path in path:
C:\Program Files\MongoDB\Server\bin

4. Create a storage location for the database file , such as C:\MongoDB. Create two folders under this folder:
db: used to store data
logs: used to store logs, create a mongolog.log file in this folder

Create another configuration file: mongod.config, the content of the file is

logpath=C:\MongoDB\logs\mongolog.log
dbpath=C:\MongoDB\db

5. Open the cmd command line and enter the following command to start the mongodb service:

C:>mongod.exe --dbpath C:/mongodb/db

Seeing that the last two letters of a bunch of things are ok, it means that the startup is successful.

6. The MongoDB service is added to the windows service. The advantage is that it can be started together with the system, and there is no need
to enter commands in the cmd (run with administrator privileges) window

C:>mongod.exe --logpath c:/mongodb/logs/mongodb.log --logappend–dbpath
c:/mongodb/db --serviceName MongoDB --install

Insert picture description here
7. Delete the MongoDB service from the windows service:

C:>mongod.exe --logpath c:/mongodb/logs/mongodb.log --logappend
–dbpath c:/mongodb/db --directoryperdb --serviceName MongoDB --remove


Introduction to MongoDB

1. What is NoSQL?

NoSQL refers to non-relational databases. NoSQL is sometimes referred to as the abbreviation of Not Only SQL, which is a general term for database management systems that are different from traditional relational databases.

NoSQL is used for the storage of very large-scale data. (For example, Google and Facebook collect trillions of data for their users every day). These types of data storage do not require a fixed model, and can be scaled horizontally without unnecessary operations.

2. What is MongoDB?

MongoDB is written in C++ and is an open source database system based on distributed file storage.
In the case of high load, adding more nodes can ensure server performance.

  • MongoDB aims to provide scalable high-performance data storage solutions for WEB applications.
  • MongoDB stores data as a document, and the data structure consists of key-value (key=>value) pairs.
  • MongoDB documents are similar to JSON objects. The field value can include other documents, arrays and document arrays.
    Insert picture description here

Three, MongoDB concept analysis

SQL terms/concepts MongoDB terms/concepts explain
database database database
table collection Database table/collection
row document Data record line/document
column field Data field/domain
index index index
table joins MongoDB terms/concepts Table connection, MongoDB does not support
primary key primary key Primary key, MongoDB automatically sets the _id field as the primary key

1. Database

Multiple databases can be created in one mongodb. The default database of MongoDB is "db", which is stored
in the data directory. A single instance of MongoDB can accommodate multiple independent databases, each with its own collection and permissions, and different databases are also placed in different files.
The "show dbs" command can display a list of all databases.
The database is also identified by name. The database name can be any UTF-8 string that meets the following conditions.

  • Cannot be an empty string ("").
  • Must not contain '' (space), ., $, /, \ and \0 (empty character).
  • Should be all lowercase.
  • Up to 64 bytes.

Some database names are reserved, and these databases with special functions can be directly accessed.

admin: From the perspective of permissions, this is the "root" database. If a user is added to this database, this user automatically inherits all database permissions. Some specific server-side commands can only be run from this database, such as listing all databases or shutting down the server.
local: This database will never be copied and can be used to store any collection limited to a single local server
config: When Mongo is used for sharding settings, the config database is used internally to store information about shards.

2. Document

A document is a set of key-value pairs (ie BSON). MongoDB documents do not need to set the same fields, and the same fields do not need the same data types. This is very different from relational databases and is also a very prominent feature of MongoDB. A simple document example is as follows:

{
   
   "site":"www.runoob.com", "age":25}

The following table lists the corresponding terms between RDBMS and MongoDB:

DBMS MongoDB
database database
form set
Row Documentation
Column Field
Table union Embed document
Primary key Primary key (MongoDB provides key as _id)
Database service and client
Mysqld/Oracle mongod
mysql/sqlplus mongo

have to be aware of is:

  1. The key/value pairs in the document are ordered.
  2. The value in the document can be not only a string in double quotes, but also several other data types (or even the entire
    embedded document).
  3. MongoDB distinguishes between type and case.
  4. MongoDB documents cannot have duplicate keys.
  5. The key of the document is a string. With a few exceptions, keys can use any UTF-8 characters.

Document key naming convention:

  1. The key cannot contain \0 (empty character). This character is used to indicate the end of the key.
  2. . And $ have special meaning and can only be used in a specific environment.
  3. The keys beginning with the underscore "_" are reserved (not strictly required).

3. Collection

A collection is a MongoDB document group, similar
to a table in RDBMS (Relational Database Management System).
The collection exists in the database, and the collection has no fixed structure, which means that you can insert
data of different formats and types into the collection, but in general, the data we insert into the collection will have a certain degree of relevance.
For example, we can insert the following documents with different data structures into the collection:

{
   
   "site":"www.baidu.com"}
{
   
   "site":"www.google.com","name":"Google"}
{
   
   "site":"www.hao123.com","name":"好 123","num":5}

When the first document is inserted, the collection is created.

Legal set name

  • The collection name cannot be the empty string "".
  • The collection name cannot contain the \0 character (empty character), which indicates the end of the collection name.
  • The collection name cannot start with "system.", which is a reserved prefix for system collections.
  • The name of the set created by the user cannot contain reserved characters. Some drivers do support the inclusion in the collection name, because some system-generated collections contain this character. Unless you want to access the collection created by this kind of system, don't appear in the name $.

Four, MongoDB data type

type of data description
String String. Data types commonly used to store data. In MongoDB, the UTF-8 encoded string is
Legal.
Integer Integer value. Used to store values. According to the server you use, it can be classified as 32-bit or 64-bit.
Boolean Boolean value. Used to store boolean values ​​(true/false).
Double Double-precision floating-point value. Used to store floating point values.
Min/Max keys Compare a value with the lowest and highest values ​​of the BSON (binary JSON) element.
Array Used to store an array or list or multiple values ​​as a key.
Timestamp Timestamp. Record the specific time when the document was modified or added.
Object For embedded documents
Null Used to create null values.
Symbol symbol. This data type is basically equivalent to the string type, but the difference is that it is generally used in languages ​​that use special symbol types.
Date Date time. Use UNIX time format to store the current date or time. You can specify your own date and time: create a Date object and pass in the year, month, and day information.
Object ID Object ID. The ID used to create the document.
Binary Binary data. Used to store binary data.
Code Type of code. Used to store JavaScript code in the document.
Regular expression Regular expression type. Used to store regular expressions.

1、ObjectId

ObjectId is similar to a unique primary key, can be quickly generated and sorted, contains 12 bytes, and its meaning is:

  • The first 4 bytes represent the creation of the unix timestamp, UTC time in Greenwich Mean Time, which is 8 hours later than Beijing time
  • The next 3 bytes are the machine identification code
  • The next two bytes consist of the PID of the process id
  • The last three bytes are random numbers

Insert picture description here
Documents stored in MongoDB must have an _id key. The value of this key can be of any type, and the default is an ObjectId object

Since the creation timestamp is saved in the ObjectId, you don't need to save the timestamp field for your document. You can get the creation time of the document through the getTimestamp function:
Insert picture description here
ObjectId is converted to a string:
Insert picture description here

2. Date

Insert picture description here

Guess you like

Origin blog.csdn.net/PILIpilipala/article/details/113810127