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 installation configuration and basic Operation of MongoDB Database

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Editor to share with you the MongoDB database installation configuration, basic operation of the sample analysis, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

The details are as follows:

1. Introduction

NO SQL:NoSQL (NoSQL = Not Only SQL), which means "not just SQL", is a general term different from the traditional relational database management system (RDBMS). NoSQL is used for very large-scale data storage. These types of data storage do not need a fixed schema and can be scaled out without redundant operations.

RDBMSNOSQL- highly organized structured data

-structured query language (SQL)

-data and relationships are stored in separate tables.

-data manipulation language, data definition language

-strict consistency

-basic transactions-represents more than just SQL

-No declarative query language

-there are no predefined patterns

-key-value pair storage, column storage, document storage

-final consistency, not ACID attribute

-unstructured and unpredictable data

-CAP theorem

-High performance, high availability and scalability

MongoDB is written in C++ language and is an open source database system based on distributed file storage. MongoDB stores the data as a document, and the data structure consists of key-value pairs (key= > value). MongoDB documents are similar to JSON objects in that you can easily query objects and arrays embedded in the document. Field values can contain other documents, arrays, and document arrays. Data mirrors can be created locally or over the network, which makes MongoDB more scalable. If the load increases (more storage space and more processing power are required), it can be distributed over other nodes in the computer network (so-called shards).

2. Installation and configuration

1. Download the windows installation package https://www.mongodb.com/download-center#community on the official website.

2. Click the installation package, install according to the instructions, and select the custom installation location of custom. Note: I failed to complete the installation when I installed version 3.6. after inquiry, I found that the installation can be completed without checking the compass box in the last step of the installation. if you need compass, click the installer again after installation, select the change option, and reinstall compass.

3, configure mongodb: create three new files in the location you want: data folder (for storing database data), logs\ mongodb.log file (for storing database logs), and etc\ mongodb.conf file (for related configuration). Open the mongodb.conf file and enter the relevant configuration information:

# Database path dbpath=D:\ MongoDB\ data# log output file path logpath=D:\ MongoDB\ logs\ mongodb.log# error log is appended. After configuring this option, the log of mongodb will be appended to the existing log file instead of creating a new file logappend=true# enable log file. Enabling journal=true# by default can filter out some useless log information. If you need to debug, set the port number to falsequiet=true# and default to 27017port=27017.

4. Install the mongo service: open the command line under the bin folder under the mongo installation directory, and enter:

Mongod-config "D:\ MongoDB\ etc\ mongodb.conf"-install-serverName "MongoDB"

-- config followed by the address of the configuration file just saved,-- serverName followed by the name of the service created

After execution, you can see the mongodb service in the Windows10 search service, and click Open:

Or execute the command line mongod-- dbpath.. / data in the MongoDB / bin directory to start the mongo service.

5, open the command line: configure environment variables, add environment variables to the bin folder of MongoDB, so that you can create a new command line window anywhere, and type mongo to open the shell command line of mongo.

3. Basic operation

1. Create / switch to database: use DatabaseName, creating a new database will not be displayed, but will not be displayed until the record is inserted into it.

Delete the database:

Db.dropDatabase ()

2. Create a collection:

Db.createCollection ('mall')

Delete the collection mall:

Db.mall.drop ()

3. Create users. Create a user for the database mall:

Db.createUser ({"user": "root", pwd: "mallroot", roles: [{role:'dbOwner',db:'mall'}]})

4. Import the JSON file into the database. Exit mongo and execute under cmd:

Mongoimport-d mall- c user-- file F:\ resource\ mall-users

Import the mall-users file into the user collection of the mall database

5. Insert a record into the collection mall:

Db.mall.insert ({"name": "Xiaomi 6", "price": "2699"})

Just like the json format, write the data you want to insert in the form of key-value pairs (key= > value).

6. Delete a piece of data:

Db.mall.remove ({mID:1005})

Delete a piece of data with a mID of 1005. Note that if {mID: "1005"} is a string type and {mID:1005} is a number type when inserting

7. Modify a piece of data:

Db.mall.update ({name: "Xiaomi 6"}, {$set: {price: "2399"}})

The first parameter in update is the condition for modifying data, and the second parameter uses $set to set the modified key value.

8. View the data in the collection:

Db.mall.find ()

Add .browse () at the end to view the data in the format.

Conditional query:

Db.mall.find ({price: {$lt:2000}})

Query entries with price less than 2000

9. Array operation

The placeholder $represents the subscript of the array, for example, updating only the elements in the cartList array that meet the criteria

User.update ({conditions}, {$set: {"cartList.$.productNum": productNum}})

$addToSet: if there is no such data in the array, add the data to the array. If there is the same array in the array, do not add it.

User.update ({conditions}, {$addToSet: {cartList:cartItem}})

$pop: delete array data, 1 from the beginning,-1 from the tail

User.update ({conditions}, {$pop: {cartList:1}})

$pull: deletes the specified data, such as the element of the specified productId in the cartList array under the specified userId

User.update ({userId:userId}, {$pull: {cartList: {productId:productId})

$push: insert an element into the array, $each traverses the array, $sort sorts the array, $slice trims the array, and $position specifies where to insert the data.

Db.test.update ({_ id: 5}, {$push: {quizzes: {$each: [{wk: 5, score: 2}, {wk: 6, score: 1}, {wk: 7, score: 3}], / / ergodic insert data $sort: {score:-1}, / / descending by score $slice: 3 / / cropping leaves only the first three pieces of data $position:2 / / insert from the second location}) The above is all the contents of the article "sample Analysis of MongoDB Database installation configuration and basic Operation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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