"MongoDB" Learning note _01

Source: Internet
Author: User
Tags sqlite database

About MongoDB Introduction

MongoDB is a product between a relational database and a non-relational database, and is the most versatile and most like relational database in a non-relational database. The data structure it supports is very loose and is a JSON-like Bson format, so you can store more complex data types.

Collection-oriented storage

In MongoDB, a database contains multiple collections, similar to a database in MySQL that contains multiple tables, and a collection that contains multiple documents, similar to a table in MySQL that contains more than one piece of data

MongoDB Basic Concept Database
    • A MongoDB can create multiple databases
    • Use show DBS to view a list of all databases
    • The Execute DB command can view the current database object or collection
    • Run the use command to connect to the specified data
Document

The document is the core of MongoDB, similar to each row of data in a SQLite database (relational database). Multiple keys and their associated values together are documents. Using a class JSON Bson to store data in Mongodb, Bson data can be understood to add some data types that are not in JSON based on JSON.

Logical connection of a document

Assume that there are two "documents" for users and addresses

{ "name": "Tom Hanks", "contact": "987654321", "DOB": "01-01-1991"}#user文档 { "building" : "A, Indiana Apt", "Pincode": 123456, "City": "Chengdu", "State ": "Sichuan"}#addres S documentation

first relationship (embedded relationship), embed the address document in the user document:

{ " name ":  "Tom Hanks",   "contact":  "987654321",   "DOB":  " building ":  " A, Indiana Apt ",  " Pincode ": 123456, " City ":   "Sichuan"} }

{ "name": "Tom Hanks", "contact": " 987654321", "DOB": "01-01-1991", "Address": [

{ "building": "A, Indiana Apt", "Pincode": 123456, "City": "Chengdu", "state": "sic Huan "},

{  "building":   "A, Indiana Apt",   "Pincode": 123456,  "City":   "Chengdu ",  " Sichuan "}

]}

Second relationship (referential relationship): Separates two documents, establishes relationships by referencing the _id field of the document

{ "contact": "987654321", "DOB": "01-01-1991", "name": "Tom benzamin", "Address_ids": [ ObjectId ("52ffc4a5d85242602e000000") #对应address文档的id字段]}

Collection

A collection is a combination of a set of documents, which is equivalent to a table in a relational database that can store documents in a different document structure in MongoDB. For example, put the above user and address of the document at the same time in a collection

Common Database operations:

1, create the database, as if the server is started, there is no direct special creation of commands, directly using the use command, such as used MyDB

2. View the currently connected database or set DB

3. View all Databases show DBS

---just started to learn a lot using use, and then show DBS don't see the new database,

None of the listed databases see MyDB or Show MyDB (empty), because MyDB is empty and there is nothing in it, MongoDB does not display or show mydb (empty)

4, delete the database using use to enter the database, and then through the Db.dropdatabase ()

Delete succeeded {"Dropped": "AA", "OK": 1}

5, create the collection, use Db.createcollection ("user")

6. View collection, show Conllections

7. Delete the collection db. The collection name. Dorp () Delete succeeded return true

8. Insert data into the collection Db.user.insert ({name: ' Niuh ', email: "[email protected]"})

9, use Save ()--how to set does not automatically create Db.user.save ({name: ' Niuh ', email: "[email protected]"})

"MongoDB" Learning note _01

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.