NoSQL database one MongoDB basic use

Source: Internet
Author: User
Tags install mongodb mongodb version relational database table

Today's web sites are becoming more and more flexible with data storage requirements, where NoSQL is becoming more prevalent in non-relational databases. The so-called non-relational database refers to a database that does not use the SQL language for data manipulation. Such a database does not have a fixed pattern for storing data, it does not support data table join operations, and can be easily scaled out. There are many types of non-relational databases, including MongoDB and Redis applications.

First, MongoDB Introduction

MongoDB is a database based on distributed file storage, between relational database and non-relational database, the most abundant function of non-relational database, most like relational database. The data structure he supports is very loose and is a JSON-like Bson format, so you can store more complex data types. MONGO's biggest feature is that the query language he supports is very powerful, and its syntax is a bit like an object-oriented query language that almost implements most of the functionality of a relational database single-table query, and also supports indexing of data.

Ii. installation of MongoDB

MongoDB installation is very simple, no need to download the source files, can be installed directly with the Apt-get command.
Open terminal and enter the following command:
sudo apt-get install mongodb
After the installation is complete, enter the following command in the terminal to view the MongoDB version:
mongo -version
Output version information to indicate successful installation

Start and close the MongoDB command as follows:

service mongodb startservice mongodb stop

The default setting MongoDB is started automatically with Ubuntu boot.
Enter the following command to see if it started successfully:
pgrep mongo -l #注意:-l是英文字母l,不是阿拉伯数字1

Uninstalling MongoDB
sudo apt-get --purge remove mongodb mongodb-clients mongodb-server

Third, using MongoDB

Shell Command mode
Enter MONGO into the shell command mode, the default connection to the database is the test database, before you must ensure that you have started MongoDB, otherwise there will be an error, after starting to run successfully, as follows:

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: "[email protected]"}

Each document has an ID field, which is the primary key that is used to uniquely determine a record. If you do not specify an ID field when inserting data into MongoDB, an ID field is automatically generated and the type of the field is ObjectId and the length is 12 bytes. The fields in the MongoDB document support strings, numbers, timestamps, and other types. A document can be up to 16M and can store quite a lot of data.

Common Operation Commands:
Database related

    • Show DBS: Display Database list
    • Show Collections: Displays a collection in the current database (similar to tables in a relational database table)
    • Show Users: Show All Users
    • Use YOURDB: Switch the current database to Yourdb
    • Db.help (): Show Database Operations Command
    • Db.yourCollection.help (): Displays the collection Action command, Yourcollection is the collection name

First try inserting a piece of data into MongoDB:

$ mongo> use shiyanlou> db.user.insertOne({name: "Aiden", age: 30, email: "[email protected]", addr: ["CD", "SH"]}){        "acknowledged" : true,        "insertedId" : ObjectId("59a8034064e0acb13483d512")}> show databases;admin      0.000GBlocal      0.000GBshiyanlou  0.000GB> show collections;user

As you can see, you switch to the Shiyanlou database by using the use directive before inserting the data, although the database temporarily does not exist, but when we insert the data, the database is automatically created. Show databases and show collection display the current database and all document collections for the current database, respectively. And after the data is inserted, the ID field is added automatically. To insert multiple data, you can use the Db.collection.insertMany method:

> db.user.insertMany([... {name: "lxttx", age: 28, email: "[email protected]", addr: ["BJ", "CD"]},... {name: "jin", age: 31, email: "[email protected]", addr: ["GZ", "SZ"]},... {name: "nan", age: 26, email: "[email protected]", addr: ["NJ", "AH"]}... ]){        "acknowledged" : true,        "insertedIds" : [                ObjectId("59a8034564e0acb13483d513"),                ObjectId("59a8034564e0acb13483d514"),                ObjectId("59a8034564e0acb13483d515")        ]}

The added data is loosely structured, and the column properties are not fixed as long as the Bson format is available, whichever is added. First define the data re-insert, you can insert multiple data at one time, run out of the above example, the library is created automatically, which also shows that MongoDB does not need to pre-defined collection, after the first insertion of data, collection will be automatically created.

Query data you can use the Db.collection.find method to specify query filtering criteria:

> Db.user.find () {"_id": ObjectId ("59a8034064e0acb13483d512"), "name": "Aiden", "age": +, "email": "[EMAIL PR Otected] "," addr ": [" CD "," SH "]} {" _id ": ObjectId (" 59a8034564e0acb13483d513 ")," name ":" Lxttx "," age ": +," email "  : "[email protected]", "addr": ["BJ", "CD"]} {"_id": ObjectId ("59a8034564e0acb13483d514"), "name": "Jin", "Age"  : "Email": "[email protected]", "addr": ["GZ", "SZ"]} {"_id": ObjectId ("59a8034564e0acb13483d515"), "name"  : "Nan", "age": +, "email": "[email protected]", "addr": ["NJ", "AH"]}> db.user.find ({name: "Jin"}) {"_id" : ObjectId ("59a8034564e0acb13483d514"), "name": "Jin", "Age": +, "email": "[email protected]", "addr": ["GZ", "SZ"]}> db.user.find ({age: {$gt: +}}) {"_id": ObjectId ("59a8034564e0acb13483d514"), "name": "Jin", "Age": +, "em Ail ":" [email protected] "," addr ": [" GZ "," SZ "]}> db.user.find ({addr:" CD "}) {" _id ": ObjectId (" 59a8034064e0a cb13483d512 ")," name ":"Aiden", "age": +, "email": "[email protected]", "addr": ["CD", "SH"]} {"_id": ObjectId ("59a8034564e0acb13483 d513 ")," name ":" Lxttx "," age ": +," email ":" [email protected] "," addr ": [" BJ "," CD "]}

In the example above, we first get all the data that was previously inserted through Db.user.find (). The query is then made using different filters, some of which are queries such as {age: {$gt: 30}}, which indicates a user with a query older than 30. It is also convenient to find out if there is an element in the query array, and the above example queries all addresses containing CD users.

MongoDB's query function is very powerful, can be combined with a variety of query conditions, more use of the method can learn other courses in the experimental building. Update data mainly through the Db.user.updateOne or Db.user.updateMany method, the former update a record, the latter update more than one record:

> 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" : "[email protected]", "addr" : [ "CD", "SH", "BJ" ] }

You can see that a record has been successfully updated. Deleting data is also very simple and can be done through the Db.user.deleteMany or Db.user.deleteOne method:

> db.user.deleteMany({addr: "CD"}){ "acknowledged" : true, "deletedCount" : 2 }> db.user.find(){ "_id" : ObjectId("59a8034564e0acb13483d514"), "user" : "jin", "age" : 31, "email" : "[email protected]", "addr" : [ "GZ", "SZ" ] }{ "_id" : ObjectId("59a8034564e0acb13483d515"), "user" : "nan", "age" : 26, "email" : "[email protected]", "addr" : [ "NJ", "AH" ] }

The above command successfully removed all the users with the "CD" address and deleted two records altogether.

NoSQL database one MongoDB basic use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.