MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications. 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.
1, compared with relational database, the advantages of MongoDB :
① Weak consistency (eventually consistent), to ensure the user's access speed:
For example, in a traditional relational database, a count " current " The exact value of the case. This is in some cases, for example through atm It's important to view your account information, But for wordnik " precise " " about " and the faster processing speed.
However, in some cases MongoDB will lock the database. If there are hundreds of requests at this point, they will accumulate and cause many problems. We have used the following optimizations to avoid locking:
We will check the records before each update. The query operation puts the object into memory, so the update is as fast as possible. In a master / Slave deployment scenario, the from node can be run with the "-pretouch" parameter, which can also have the same effect.
Use multiple mongod processes. We split the database into multiple processes based on the access pattern.
The ② document structure is stored in a way that makes it easier to get data.
For a hierarchical data structure, if you want to use a flat, tabular structure to hold the data, it is difficult to query or retrieve data.
Example 1:
Just take one."Dictionary Items", though not very complex, it will still be related to"definition","Part of speech","pronunciation"or"References"and other content. Most engineers will use this model as a primary key and foreign key in a relational database, but think of it as a"Document"and not"a series of related tables"wouldn't it be better? Use"dictionary.definition.partofspeech= ' noun '"query is also easier and faster than a series of complex (and often expensive) connection queries between tables.
Example 2: In a relational database, a blog (a poll that contains the content, comments, and comments) is scattered across multiple data tables. In MongoDB , you can use a document to represent a blog, comments and polls as an array of documents, placed in the main body document. This makes data easier to manage and eliminates "JOIN" operations that affect performance and horizontal scalability in traditional relational databases .
Code↓
> Db.blogposts.save ({title: "My first Post", Author: {Name: "Jane", id:1},
Comments: [{by: "Abe", Text: "First"},
{by: "Ada", Text: "Good post"}]
})
> Db.blogposts.find ({"Author.name": "Jane"})
> Db.blogposts.findOne ({title: "My First Post", "Author.name": "Jane",
Comments: [{by: "Abe", Text: "First"},
{by: "Ada", Text: "Good post"}]
})
> Db.blogposts.find ({"comments.by": "Ada"})
> Db.blogposts.ensureIndex ({"Comments.by": 1});
Example ③:
MongoDB is a document-oriented database, currently developed and maintained by 10gen, it is rich in functionality, complete, can completely replace MySQL. in the process of using MongoDB to prototype a product, we summarize Some of the highlights of Monogdb:
Use JSON -style syntax to be easy to master and understand:MongoDB uses a variant of JSON BSON As the internal storage format and syntax. the JSON -style syntax is used for MongoDB operations , and the data submitted or received by the client is presented in JSON form. Compared to SQL , it is more intuitive, easy to understand and master.
Schema-less, supporting embedding of sub-documents:MongoDBis aSchema-freedocument database. A database can have multipleCollection, eachCollectionis aDocumentsthe collection. Collectionand theDocumentand the traditional databaseTableand theRownot equal. No need to define beforehandCollectionand can be created at any time.
Collection can contain document records with different schemas . This means that the document in your previous record has 3 Properties, and the next record can have a document of ten properties, the type of the property can be a basic data type (such as a number, a string, a date, and so on). It can also be an array or a hash, or even a subdocument (embeddocument). In this way, the inverse normalization (denormalizing) data model can be implemented to improve the speed of the query.
The ③ built-in Gridfssupports high-capacity storage.
Gridfs is an excellent distributed file system that can support massive data storage.
Built-in gridfs , MongoDB, can meet the fast range of large data set query.
④ built-in sharding.
Provides a range - based Auto sharding mechanism: A collection can be divided into segments according to the range of records, Slice to different Shard .
Shards can be combined with replication, with replica sets can implement sharding+fail-over , different shard mapreduce And so on, these will be mongodb auto route to backend data node. This allows us to focus on our own business and, when appropriate, to upgrade without pain. mongodb sharding design capability supports up to approximately 20 petabytes
This ensures that MongoDB runs on a cheap PC server cluster. PC clusters are easy to expand and inexpensive to avoid the complexity and cost of "sharding" operations.
⑤ Third-party support is plentiful. ( This is the advantage ofMongoDB comparedto other NoSQL )
Many of the NoSQL Open source databases on the Web are entirely community-based, with no official support, and pose a significant risk to users.
and the open source document database MongoDB behind the commercial company 10gen provides it for business training and support.
And the MongoDB Community is very active, and many development frameworks quickly provide support for mongdb . Many well-known big companies and Web sites also use MongoDB in production environments , and more and more innovative companies are using MongoDB as A and Django ,RoR to match the technical solution.
⑥ Superior Performance:
In the case of use, tens other document objects, near10Gthe data that is indexed to theIDthe query is no more thanMySQLis slow, and queries against non-indexed fields are all-in-one. MySQLthe actual inability to query any field under large data volume, andMongoDBI'm amazed at the query performance. Write performance is also very satisfying, also write millions data,MongoDBthan I've tried before.Couchdbmuch faster, basically.Tenminutes and below can be resolved. To make up a sentence, in the course of observationMongoDBIt's not all that much.CPUThe killer.
2, compared with relational database,MongoDB Disadvantage:
①mongodb does not support transactional operations.
So a strict system of transactions (if the banking system) must not be able to use it. ( This and the merit ① are corresponding )
②mongodb occupies too much space.
For its reasons, in the official FAQ , the following aspects are mentioned:
1, space pre-allocation: To avoid excessive hard disk fragmentation, mongodb 128m 256m The exponential increments until the 2g
2, the space occupied by the field name: In order to maintain the structure information within each record for the query, key-value bson value Span style= "Font-family:verdana;" >key is the largest. One way to reduce space usage is to take the field names as short as possible, so that space is small, but this requires a tradeoff between legibility and space occupancy. I have suggested that the author of the field be a index
3, delete the record does not free space: This is easy to understand, in order to avoid the record deleted after the large-scale movement of data, the original record space is not deleted, only marked " deleted " can be reused later.
4, can regularly run db.repairdatabase () to organize records, but this process will be relatively slow
③mongodb does not have a proven maintenance tool like MySQL , which is a noteworthy area for development and it operations.
Introduction to MongoDB Database