Introduction to MongoDB

  1. Install

    You need to download the package. After decompression and installation, you need to create a \data\db file in the root directory of the C drive to store the data: cd C:\
    md "\data\db" . The way to start Mongodb is to enter the command line in the folder C:\Program Files\MongoDB\Server\5.0\ bin, and then enter the command:

    1
    mongod --dbpath c:\data\db

    After the server starts, you can find Listening on address: 127.0.0.1, port: 27017 (default port number) in the pop-up information. At this point, enter the following command to connect to the database. The interface at this time is the mongo shell, which is based on the JS engine. The cls command clears the screen.

    1
    C:\mongodb\bin\mongo.exe

    Another startup method is the configuration file, which is used during deployment.


  1. SQL and non-SQL

    Another understanding of non-SQL (or no SQL) is not only SQL. It can not only do the functions of relational databases, but also functions that relational databases cannot. There are four main categories of non-SQL:

    1. document-oriented: data is stored in the form of documents, such as mongodb, dynamodb, firebase
    2. key-value: cache database, such as redis
    3. graph-oriented: a database (graph theory) based on graphs, such as neo4j
    4. column-family: for big data, such as cassandra

  1. Mongodb introduction

    Mongo comes from humongous (huge) and is used to process huge data.

    Documents are stored in a json-like (key-value pair) form, which is strictly called BSON (binary JSON). It has some types that json does not support, such as _id: ObjectId('xxx') does not exist in json, it is a type created by mongodb.

    1
    2
    3
    4
    {
    _id: ObjectId('xxx'), //A unique identifier used to represent data.
    name: 'yyy'
    }

  1. Flexibility or schemaless

    Schema is the design diagram of the data, which defines which properties and fields the data has. Schema is not a strictly prescribed design diagram, so data can be stored without schema.

    For example, collection (collection, corresponding to table in sql), contains multiple data of the same type (document), but there is no hard requirement, and the data can also be of different types. The only thing to make sure is that the _id (data identifier) cannot be repeated, like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [
    {
    _id: ObjectId('xxx1'),
    name: 'yyy'
    },
    {
    _id: ObjectId('xxx2'),
    name: 'yyy'
    },
    {
    _id: ObjectId('xxx3'),
    username: 'yyy' //Different data types can also be received.
    }
    ]

    However, we will avoid doing this during development, and still ensure that the data has a fixed structure for easy traversal.


Share