Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of basic Operation of MongoDB Database

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail the example analysis on the basic operation of MongoDB database. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In order to save the user data and business data of the website, a database is usually needed. MongoDB and Node.js are a good match because Mongodb is a non-relational database based on documents, documents are stored in BSON (JSON's lightweight binary format), and commands for managing databases such as additions, deletions, modifications and queries are very similar to JavaScript syntax.

Database

Database, as its name implies, is a warehouse for data storage, with two main functions.

1. Store data in an organized way

Unlike storing files on disk, the database organizes the storage form of data for users. Users only need to write the data according to the interface provided by the database, and the data will be stored in a standard format.

2. Query according to different needs

The database should not only be able to write data, but also support data query, and be able to query according to different needs. Because the storage is organized, queries can be more standardized and much faster

The difference between different databases is that they store data in different organizations and provide different kinds of queries at the same time. Users can choose the appropriate database according to their own needs.

[classification]

There are many categories of databases, which can be divided into the following two categories according to the support for the SQL language:

1. SQL databases, such as Oracle, Mysql, etc.

2. NoSQL databases, such as Redis, MongoDB, etc.

With the emergence of large-scale Internet applications, the traditional SQL database has encountered some design drawbacks. For example, SQL's definition of tables makes the application inflexible and makes it difficult to scale out. Compared with some features that are difficult to satisfy, many features of SQL database do not have the opportunity to show their talents. For example, in many scenarios, timely access is not necessary and there are not many transaction requirements, and these additional features consume the performance of the SQL database

So the NoSQL database arises at the historic moment, and the full name of NoSQL is Not Only SQL, which means "not just SQL". But in fact, most NoSQL databases give up support for the SQL language. Compared with SQL relational databases, NoSQL non-relational databases mostly give up some features. For example, real-time consistency, full support for transactions, and multi-table queries are abandoned. It sounds like there are many disadvantages, but the benefits are also obvious. NoSQL database is simple and convenient, easy to expand, and has better performance.

Overview

MongoDB is an open source NoSQL database, which is called mango database in China. Linux, Apache, MySQL, and PHP make up the very famous LAMP architecture. Now, it is proposed that the MySQL representing M in LAMP be replaced with MongoDB

There are many NoSQL databases, so why choose MongoDB?

MongoDB is a database based on distributed file storage. Written in C++ language, it aims to provide scalable high-performance data storage solutions for WEB applications. MongoDB uses collection and document to describe and store data. Collection is equivalent to table, document is equivalent to row, field is equivalent to column, unlike relational database such as MySQL, table structure is fixed, for example, a row consists of several columns, rows are all the same, while MongoDB is different, multiple documents in a collection can have different structures and are more flexible.

MongoDB has its own distinctive features, which can be summed up as follows

1. There is no restriction on the table structure

In the traditional SQL database, the table structure needs to be defined for each table. If there are new storage requirements, you often need to add new fields and change the table structure. In some scenarios, it can be inconvenient, but for MongoDB, this is no longer a problem. Because it does not have the concept of a table structure, there is no need to initialize a table before using it. This feature of MongoDB is suitable for rapid development and changing business requirements.

2. Full index support

Some NoSQL databases, such as redis, are in-memory databases that are fast. However, as a key-value database, only one way of keystroke query is supported. Flexibility, scope of use, and ease of use are all affected; hbase, for example, writes very fast. However, the query is also limited, it only supports a single index, and the secondary index needs to be implemented on its own.

MongoDB supports single-key index, multi-key index, full-text index and geolocation index. Therefore, MongoDB is a very complete NoSQL database, which is also called the non-relational database closest to the relational database.

3. Good data security and convenient scale expansion

MongoDB uses replication sets to store multiple copies to ensure the security of the data. At the same time, the built-in slicing technology of MongoDB can easily expand the data scale. Slicing technology is a very novel feature. it includes a series of tasks that require a lot of manual operations in other databases, such as automatic data interface, dynamic capacity expansion and capacity reduction, and provides a unified access to the database. There is no need for redistribution in the application layer, which significantly reduces labor costs.

4. Perfect documentation support and driver support

Installation

First of all, select the appropriate MongoDB version to download from the download page of the official website.

Then, install it step by step

By default, it is installed in the MongoDB folder under the Program Files folder of disk C.

Server configuration

[build server]

To build a server, you need to take the following steps

1. Create a data folder to store the database's data files; create a log folder to store the database's log files; create a bin folder to store the database's executable files; and create a conf folder to store the database's configuration files

2. Under the windows system, you need to set the environment variable, otherwise you will prompt that the mongod command is not available on the command line

In the path of the environment variable, add the directory of the mongod.exe file

3. Next, there are two ways to start the mongoDB service. One is shown below. Set the dppath parameter value to the custom directory path.

Mongod-dbpath=D:/app/mongo/data

As you can see from the following figure, the default port for mongodb is 27017

4. The other is to create a new mongod.conf file under the conf folder, in which the configuration parameters for mongodb startup will be set.

Dbpath = datalogpath = log/mongod.logmongod-f conf/mongod.conf

This method does not have any hints in the command line tool because the record has been saved to the log file and the mongodb service has been started normally

[connect to server]

After setting up the mongodb server, you need to use the client mongo to connect before you can proceed to the next step.

Because you are using mongo to connect to the mongodb server, you need to ensure that the command-line tool that starts the mongodb server is not closed, open a new command-line tool, and type mongo 127.0.0.1 hand testline test as the name of the database

[shut down mongod service]

First switch to the admin database (use admin), and then use the db.shutdownServer () command to shut down the service

Database operation

[default]

The default database in MongoDB is test. If no new database is created, the collection will be stored in the test database.

[view]

Use show dbs to view the database

Show dbs

[create / switch]

When you use the use command to switch / create a database, you will find that the created database is not in the list of the database. To display it, you need to insert some data into the database.

Use db_name

[show current database]

Use the db command to display the current database

Db

[write data to the collection]

Use db. The name of the collection. Insert (document) to write the data of the document to the collection in the format of JSON. All data stored in the collection is in BSON format. BSON is a kind of json-like binary storage format, referred to as Binary JSON.

Db.collection_name.insert ()

[view Collection]

The insert operation above automatically creates a collection db1_coll1, and you can view all collections in the current database using the show collections command.

[delete database]

This will delete the currently selected database. If no database is selected, it will delete the default 'test' database

Db.dropDatabase ()

Set operation

The collection is similar to the data table in the SQL database and is identified as collection

[view Collection]

You can use the command show collections to check the collection created

[note] you can also use show tables to view collections

[create Collection]

When inserting a document, MongoDB first checks the size of the upper limit collection capped field, and then checks the max field

Db.createCollection (name, {capped:, autoIndexId:, size:, max})

Name: the name of the collection

Capped: whether to enable set restrictions. If you need to set a restriction if enabled, it is not enabled by default. This parameter has no practical significance.

Max: the maximum number of entries in the collection. There is no limit by default.

Size: limit the size of the space used by the collection. By default, there is no limit. Size has a higher priority than max.

AutoIndexId: whether to use _ id as the index. Default is to use (true or false)

[note] when you insert a document into a collection, if the collection does not exist, the collection is automatically created

[delete collection]

The db.collection_name.drop () of MongoDB is used to delete the collection from the database. If the selected collection is successfully deleted, the drop () method returns true, otherwise it returns false

This is the end of this article on "sample Analysis of the basic Operation of MongoDB Database". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report