About MongoDB (i)

Source: Internet
Author: User
Tags findone relational database table

Nosql DB

This is a non-relational database. We typically have three types of databases: relational database (RDBMS), Online Analytical Processing database (OLAP), and Philippine Relational Database (NOSQL).

MongoDB belongs to the third type, and is a document-oriented database.

Pros and cons

Advantage: Query performance is significantly faster than MySQL when no index is established. is not slower than MySQL after the index has been built.

Disadvantage: Do not support things. Therefore, this database is not suitable for situations where things are highly demanding.

Concepts & Syntax Concepts

Collection: Collection, equal to table in relational database.

Dcoument: Document object, equivalent to a row of records in a relational database table.

Field: A key-value pair, equivalent to a column of table in a relational database

Database: This is no different.

Data is stored in MongoDB in Bson format, so-called Bson is binary JSON, which is binary JSON data.

Grammar

Properties : JavaScript syntax.

Display Database : Show DBS

using the database: Use your_db (Note: This is good, your_db does not exist, then "new" one out, no error, the fact is: collection and database is not created until the Docuemnt object is stored)

Insert Record : Your_db.your_collection.insert (JSON expression) (Note: You can store the associated object through a JSON expression in a document, as shown in callout 3 in---2. This is different from the relational database)

Update record : Your_db.your_collection.update (JSON expression)

Your_db.your_collection.save (JSON expression) : is actually a merge of the insert and update operations, the record does not exist, the insert is present, then update.

The above instruction shows as follows:

Figure 1

Query Record : Your_db.your_collection.find ({key value pair},{need to show/hide key value pair})/findone ({key value pair},{the key value pair} for fields to be shown/hidden)

Note : A) Find returns a Cursor object (Cursor object), and FindOne returns a document (a record).

The results of find can be formatted with foreach (Printjson), as shown in Figure 1:

Figure 2

b). FindOne, for more than one record, only the first record is returned (the oldest one).

c). The second parameter: {key value pair} for fields to be shown/hidden, or set to whitelist, which fields are allowed, such as {' Field1 ': 1, ' field2 ': 1,...},

Either set to blacklist, that is, specify which fields are not displayed, such as {' field1 ': 0, ' field2 ': 0,....}

Cannot be mixed, for example {' field1 ': 1, ' Field2 ': 0}, this is not supported.

However, for the _id field exception, this field can be mixed.

d). Since multiple objects may be stored in a document, the reference sub-object can be used as a dot (.) in the same way as JS.

For example, it is known that the name field is stored in the user's document, and this field is a JSON object (another table-in a relational database), as follows

{  "_id": ObjectId ("A2349723424adfa14"),    "name": {"First": "John", "Last": "Smith"}     ....}

So, if you want to query a person named John, you can write JS

Your_db.user.find ({' Name.first ': ' John '})

  

e). Paradigm (normalization) and inverse paradigm (de-normalization)

Usually the data is stored in a document, this is called de-normalization, that is, in exchange for the performance of redundancy for reading. This is often used for more reading data, such as CMS systems.

If you write more, it's best to normalization the data Table (collection) by first, second, and third paradigm.

MongoDB is to support normalization, through the implementation of _id (Figure 2, callout 2), _id a special algorithm, taking into account the machine, database, collection, time and other factors, it is integrated into the _id, to ensure the uniqueness of the data.

In the database after normalization, point to other docuemt through link.

For example, the book object can have a link to the author object, as follows:

{"_id": ObjectId ("book_id1123132"),

"Name": "MongoDB 30 minutes proficient",

"author_id": ObjectId ("Author_id_say_tommy"),

...

}

Author Document object:

{

"_id": ObjectId ("Author_id_say_tommy"),

"Name": "Tommy",

...

}

This is the separation of redundant data, which facilitates the operation of update data.

About MongoDB (i)

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.