In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Nowadays, websites have more and more flexible requirements for data storage, under this demand, NoSQL, that is, non-relational databases, is becoming more and more popular. The so-called non-relational database refers to the general name of the database that does not use the SQL language for data operation. This kind of database does not have a fixed schema when storing data, does not support the operation of the data table join, and can be easily expanded horizontally. There are many kinds of non-relational databases, among which MongoDB and Redis are widely used.
1. Introduction of MongoDB
MongoDB is a database based on distributed file storage, which lies between relational database and non-relational database. It is the most functional non-relational database and most similar to relational database. The data structure he supports is very loose, which is similar to json's bson format, so it can store more complex data types. The most important feature of Mongo is that the query language it supports is very powerful, and its syntax is somewhat similar to the object-oriented query language. It can almost achieve most of the functions similar to the single table query of relational database, and also supports the establishment of data indexing.
Second, install MongoDB
MongoDB installation is simple, without downloading the source file, you can install it directly with the apt-get command.
Open the terminal and enter the following command:
Sudo apt-get install mongodb
After the installation is complete, enter the following command at the terminal to view the MongoDB version:
Mongo-version
Output version information, indicating that the installation was successful
The command to start and close mongodb is as follows:
Service mongodb startservice mongodb stop
The default setting, MongoDB, starts automatically with Ubuntu startup.
Enter the following command to see if the startup is successful:
Pgrep mongo-l # Note:-l is the letter l, not the Arabic numeral 1
Uninstall MongoDB
Sudo apt-get-purge remove mongodb mongodb-clients mongodb-server
Third, use MongoDB
Shell command mode
Enter mongo to enter shell command mode. The database connected by default is test database. Make sure you have started MongoDB before this, otherwise an error will occur and run successfully after startup, as shown below:
The document record stored by MongoDB is a BSON object, similar to a JSON object, consisting of key-value pairs. For example, a user record:
{name: "Aiden", age: 30, email: "luojin@simplecloud.cn"}
Each document has an id field, which is the primary key and is used to uniquely identify a record. If no id field is specified when inserting data into MongoDB, an id field is automatically generated with a type of ObjectId and a length of 12 bytes. Support strings, numbers, timestamps and other types in the fields of MongoDB documents. A document can be up to 16m in size and can store a considerable amount of data.
Common operation commands:
Database correlation
Show dbs: display database list show collections: show collections in the current database (similar to tables in a relational database table) show users: show all users use yourDB: switch the current database to yourDBdb.help (): display database operation commands db.yourCollection.help (): show collection operation commands, yourCollection is the collection name
First try to insert a piece of data into the MongoDB:
$mongo > use shiyanlou > db.user.insertOne ({name: "Aiden", age: 30, email: "luojin@simplecloud.cn", addr: ["CD", "SH"]}) {"acknowledged": true, "insertedId": ObjectId ("59a8034064e0acb13483d512")} > show databases;admin 0.000GBlocal 0.000GBshiyanlou 0.000GB > show collections;user
As you can see, we switched to the shiyanlou database using the use instruction before inserting the data, and although the database does not exist for the time being, it is automatically created when we insert the data. Show databases and show collection show the current database and all the document collections of the current database, respectively. And after the data is inserted, the id field is automatically added. To insert multiple pieces of data, you can use the db.collection.insertMany method:
> db.user.insertMany ([. {name: "lxttx", age: 28, email: "lxttx@simplecloud.cn", addr: ["BJ", "CD"]},... {name: "jin", age: 31, email: "jin@simplecloud.cn", addr: ["GZ", "SZ"]},... {name: "nan", age: 26, email: "nan@simplecloud.cn", addr: ["NJ" "AH"]}.]) {"acknowledged": true, "insertedIds": [ObjectId ("59a8034564e0acb13483d513"), ObjectId ("59a8034564e0acb13483d514"), ObjectId ("59a8034564e0acb13483d515")]}
The structure of the added data is loose, as long as it is in bson format, the column properties are not fixed, according to the added data shall prevail. After defining data before inserting, you can insert multiple pieces of data at one time. After running the above example, the library is automatically created. This also shows that MongoDB does not need to pre-define collection. After inserting data for the first time, collection will be created automatically.
Query data can be queried using the db.collection.find method, and query filtering criteria can be specified:
> db.user.find () {"_ id": ObjectId ("59a8034064e0acb13483d512"), "name": "Aiden", "age": 30, "email": "luojin@simplecloud.cn", "addr": ["CD", "SH"]} {"_ id": ObjectId ("59a8034564e0acb13483d513"), "name": "lxttx", "age": 28, "email": "lxttx@simplecloud.cn" "addr": ["BJ", "CD"]} {"_ id": ObjectId ("59a8034564e0acb13483d514"), "name": "jin", "age": 31, "email": "jin@simplecloud.cn", "addr": ["GZ", "SZ"]} {"_ id": ObjectId ("59a8034564e0acb13483d515"), "name": "nan", "age": 26 "email": "nan@simplecloud.cn", "addr": ["NJ", "AH"]} > db.user.find ({name: "jin"}) {"_ id": ObjectId ("59a8034564e0acb13483d514"), "name": "jin", "age": 31, "email": "jin@simplecloud.cn", "addr": ["GZ" "SZ"]} > db.user.find ({age: {$gt: 30}}) {"_ id": ObjectId ("59a8034564e0acb13483d514"), "name": "jin", "age": 31, "email": "jin@simplecloud.cn", "addr": ["GZ", "SZ"]} > db.user.find ({addr: "CD"}) {"_ id": ObjectId ("59a8034064e0acb13483d512") "name": "Aiden", "age": 30, "email": "luojin@simplecloud.cn", "addr": ["CD", "SH"]} {"_ id": ObjectId ("59a8034564e0acb13483d513"), "name": "lxttx", "age": 28, "email": "lxttx@simplecloud.cn", "addr": ["BJ", "CD"]}
In the above example, we first obtained all the previously inserted data through db.user.find (). Queries are then made using different filtering criteria, and some of them, such as {age: {$gt: 30}}, indicate that users older than 30 are queried. You can also find it convenient to query whether an element exists in the array. In the above example, all addresses contain CD users.
MongoDB query function is very powerful, can combine a variety of query conditions, more methods of use can learn other courses in the experimental building. Data is updated mainly through db.user.updateOne or db.user.updateMany methods, the former updating one record and the latter updating multiple records:
> db.user.updateOne (. {name: "Aiden"},... {$set: {age: 29, addr: ["CD", "SH", "BJ"]}}.) {"acknowledged": true, "matchedCount": 1, "modifiedCount": 1} > db.user.find ({name: "Aiden"}) {"_ id": ObjectId ("59a8034064e0acb13483d512"), "name": "Aiden". "age": 29, "email": "luojin@simplecloud.cn", "addr": ["CD", "SH", "BJ"]}
You can see that a record has been successfully updated. Deleting data is also very simple, through the db.user.deleteMany or db.user.deleteOne methods:
> db.user.deleteMany ({addr: "CD"}) {"acknowledged": true, "deletedCount": 2} > db.user.find () {"_ id": ObjectId ("59a8034564e0acb13483d514"), "user": "jin", "age": 31, "email": "jin@simplecloud.cn", "addr": ["GZ", "SZ"]} {"_ id": ObjectId ("59a8034564e0acb13483d515") "user": "nan", "age": 26, "email": "nan@simplecloud.cn", "addr": ["NJ", "AH"]}
The above command successfully deletes all users whose addresses contain "CD", deleting a total of two records.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.