[MangoDB] (c) Basic operation MangoDB

I. Introduction

MongoDB is written in C ++ language, it is an open source database distributed file system based storage.

In the case of high load, add more nodes, you can ensure server performance.

MongoDB is designed to provide scalable, high-performance data storage solution for WEB applications.

MongoDB the data is stored as a document data structure of the key (key => value) pairs. MongoDB document similar to JSON object. Field value can contain other documents, arrays and array of documents.

If you are interested in the history of development for MongoDB, you can refer to the next is not a technology born perfect, MongoDB decades of development Quan Jilu this article.
Here Insert Picture Description

Two, MongoDB features

  • Free mode: You can store different structure of the document in the same database
  • Set-oriented storage: a form suitable for storage JSON-style documents
  • Full index support: any property index
  • Replication and high availability: support data replication between servers do, support the master copy from each other between the patterns and the server object to provide redundancy and automatic failover
  • Automatic fragmentation: the level of support database cluster, can dynamically add additional machines
  • Rich query: rich query expressions, query instruction form using JSON markup and query document embedded objects and arrays
  • Fast-place update: query optimizer analyzes the query expression, and generate an efficient query plan
  • Efficient conventional storage methods: support for binary large object data sets (such as: picture or photo)

Third, the basic model

  • MongoDB stored as a data file, a data structure composed of the key (key => value)
  • MongoDB document similar to JSON objects, field value can contain other documents, arrays, array of documents
  • Installation Management mongodb environment, complete database, collection management
  • Increased data, modify, delete, query
SQL belonging / concept The term & MongoDB / concepts explain
database database database
table collection Database table / collection
row document Data rows / documents
column field Data field / fields
  • database database, a SQL database (database) the same concept, a database containing a plurality of sets (Table)
  • collection set, corresponding to the SQL table (Table), a set of a plurality of documents can be stored (rows). Difference is that a set of structure (schema) is dynamic, not required to declare a strict pre-table structure. More importantly, the data by default MongoDB do not have to write a check in any schema.
  • document file, SQL equivalent rows (Row), a document consists of a plurality of fields (columns) composition, and use bson (json) notation.
  • field field, the equivalent of SQL columns (column), the difference is that compared to ordinary column type field can be more flexible, such as support for nested documents array.
    Further, in MongoDB Type field is fixed, a case-sensitive, and the field in the document is ordered.

In addition, there are other concepts SQL, correspondence is as follows:

SQL concepts MongoDB concept
primary key _id
foreign key reference
view view
index index
join $lookup
transaction trasaction
group by aggregation
  • _id primary key, MongoDB using a _id field to ensure the uniqueness of the default document.
  • reference cited, barely correspond to the concept of foreign key (foreign key), the reason is reluctant because the reference did not implement any foreign key constraints, but merely associating a query by the client (driver) automatically, a special type of conversion .
  • view view, MongoDB 3.4 began to support the view, and the SQL view there is no difference, the view is based on the object layer of dynamic queries on the table / collection, can be virtual, it can be physical (materialized views).
  • index index, with the index of the same SQL.
  • Lookup $, which is an aggregation operator, may be used to implement the functions similar to SQL-join connecting
  • transaction services, from MongoDB 4.0 version, provides support for transactions
  • aggregation polymerization, MongoDB provides a powerful computing framework polymerization, group by type is one of a polymerization operation.

BSON data types

MongoDB can use the Javascript document object representation from a format perspective, it is JSON-based.

A typical document as follows:

{
  "_id": 1,
  "name" : { "first" : "John", "last" : "Backus" },
  "contribs" : [ "Fortran", "ALGOL", "Backus-Naur Form", "FP" ],
  "awards" : [
    {
      "award" : "W.W. McDowell Award",
      "year" : 1967,
      "by" : "IEEE Computer Society"
    }, {
      "award" : "Draper Prize",
      "year" : 1993,
      "by" : "National Academy of Engineering"
    }
  ]
}

Once, JSON and the emergence of popular Web 2.0 allows data transfer becomes very simple, so use JSON syntax is very easy for developers to accept.

But JSON also has its own shortcomings, such as not to support a particular type of data such as date, so actually using MongoDB is an extension of JSON type, called BSON (Binary JSON).

BSON supported data types include:

Distributed ID

In stand-alone era, most applications use the database to increment type ID as the primary key. The traditional RDBMS also support this approach, such as mysql can be implemented primary key increment by declaring auto_increment. But once distributed data storage achieved, this approach is no longer applicable, the primary reason is that the key can not be guaranteed on the plurality of nodes are not duplicated.

In order to achieve uniqueness guarantee distributed data ID, application developers presented their own programs, and the ID segment will generate the majority of scenarios, such as the famous snowflake algorithm to simultaneously use a timestamp, machine number, process ID and a random number to ensure uniqueness.

MongoDB uses ObjectId to indicate the type of the primary key, the database each document has a _id field indicates the primary key.

_id generation rule as follows:

Here Insert Picture Description
These include:

  • 4-byte Unix timestamp
  • 3-byte machine ID
  • 2-byte process ID
  • 3-byte counter (random initialization)

It is worth mentioning that the _id generated essentially by the client (Driver) generated, so you can get better randomness, while reducing the load of the server.

Of course, the server will detect document contains written _id field, if not a generation.

Third, the operation syntax

In addition to the document model itself, for the operation command data it is also based on the syntax JSON / BSON format.

For example, insert the document:

db.book.insert(
{
  title: "My first blog post",
  published: new Date(),
  tags: [ "NoSQL", "MongoDB" ],
  type: "Work",
  author : "James",
  viewCount: 25,
  commentCount: 2
}
)

Find the documentation:

db.book.find({author : "James"})

Update command of the document:

db.book.update(
   {"_id" : ObjectId("5c61301c15338f68639e6802")},
   {"$inc": {"viewCount": 3} }
)

Delete command of the document:

db.book.remove({"_id":
     ObjectId("5c612b2f15338f68639e67d5")})

In conventional SQL syntax can be defined fields returned, MongoDB can be represented Projection:

db.book.find({"author": "James"}, 
    {"_id": 1, "title": 1, "author": 1})

Simple paging query:

db.book.find({})
    .sort({"viewCount" : -1})
    .skip(10).limit(5)

For a complete contrast documents with SQL mode operation, the official document was described in more detail:
https://docs.mongodb.com/manual/reference/sql-comparison/

MongoDB's a lot of benefits, such as multi-column index, you can use some statistical query functions, support for multi-criteria query, but currently does not support multi-table queries, you can find ways to solve the problem through multi-table query data redundancy.

MongoDB operation on the data is very rich, do some illustration below, most of the content from the official documents.

All data query colls

db.colls.find() 
//select * from colls        

By specifying a query condition

db.colls.find({‘last_name’: ‘Smith’});
//select * from colls where last_name=’Smith’

Designated multi-criteria query

db.colls.find( { x : 3, y : “foo” } );
//select * from colls where x=3 and y=’foo’

Conditions specified range queries

db.colls.find({j: {$ne: 3}, k: {$gt: 10} });
//select * from colls where j!=3 and k>10

Query does not include a content

db.colls.find({}, {a:0});
//查询除a为0外的所有数据

Support <, <=, >, >=queries, are required symbol substitution$lt,$lte,$gt,$gte

db.colls.find({ “field” : { $gt: value } } ); 

db.colls.find({ “field” : { $lt: value } } ); 

db.colls.find({ “field” : { $gte: value } } );

db.colls.find({ “field” : { $lte: value } } );

Range queries can also be done on a field

db.colls.find({ “field” : { $gt: value1, $lt: value2 } } );

Is not equal to the query with character$ne

db.colls.find( { x : { $ne : 3 } } );

$ characters in queries in

db.colls.find( { “field” : { $in : array } } );

db.colls.find({j:{$in: [2,4,6]}});

not in the query with the characters $ nin

db.colls.find({j:{$nin: [2,4,6]}});

Modulo queries characters $ mod

db.colls.find( { a : { $mod : [ 10 , 1 ] } } )
// where a % 10 == 1

$ All queries

db.colls.find( { a: { $all: [ 2, 3 ] } } );
//指定a满足数组中任意值时

$ Size Queries

db.colls.find( { a : { $size: 1 } } );
//对对象的数量查询,此查询查询a的子对象数目为1的记录

$ Exists inquiry

db.colls.find( { a : { $exists : true } } ); 
// 存在a对象的数据

db.colls.find( { a : { $exists : false } } ); 
// 不存在a对象的数据

$typeQuery $typetype value data is bson

db.colls.find( { a : { $type : 2 } } ); 
// 匹配a为string类型数据

db.colls.find( { a : { $type : 16 } } ); 
// 匹配a为int类型数据

Use regular expression matching

db.colls.find( { name : /acme.*corp/i } );
//类似于SQL中like

Embedded object query

db.colls.find( { “author.name” : “joe” } );

Version 1.3.3 and later includes $ not query

db.colls.find( { name : { $not : /acme.*corp/i } } );

db.colls.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );

sort () to sort

db.colls.find().sort( { ts : -1 } );
//1为升序2为降序

limit () returns the number of restrictions on the query data

db.colls.find().limit(10)

Skip () skip certain data

db.colls.find().skip(10)

snapshot () returns a snapshot to ensure there is no duplication of data or missing objects

() Query statistics count the number of objects

db.students.find({‘address.state’ : ‘CA’}).count();
//效率较高

db.students.find({‘address.state’ : ‘CA’}).toArray().length;
//效率很低

group () SQL query result and the group by a similar function

DISTINCT () Returns the unique values

Fourth, the index

Undoubtedly, the key is the ability to index a database, MongoDB supports a very rich index type. With these indexes, you can achieve fast data lookup, and the type and characteristics of the index for different application scenarios designed.

Index depends on the underlying technology of storage engine, MongoDB wiredTiger used as the default in the current version of the engine.
B + tree structure used in the realization of the index, which is the other traditional database is no different.

So this is good news, mostly based on some index tuning tips SQL database is still viable on MongoDB.

Here Insert Picture Description
Use ensureIndexes can declare a common set of index:

db.book.ensureIndex({author: 1})

Numeral 1 represents the latter author ascending, or descending if it is -1

Composite Index (Compound) is achieved, as follows:

db.book.ensureIndex({type: 1, published: 1})

Only for the composite index, the key index of the order only becomes meaningful

If the index field is an array type, which automatically index to an array (multikey) Index:

db.book.ensureIndex({tags: 1})

MongoDB can contain an array of fields on the composite index, but only contain a maximum of

Index feature

When you declare an index, it can also give a certain characteristic parameters for the index by a number of options, including:

  • unique = true, indicates a unique index
  • expireAfterSeconds = 3600, which is a TTL indicates index data and the aging after 1 hour
  • sparse = true, sparse represents the index, only non-empty (non-null) field in the document
  • partialFilterExpression: {rating: {$ gt: 5}, the conditional expression index, calculation conditions is met before the document index

Index Classification

In addition to the general index, MongoDB also supported types include:

  • Hashes (HASH) index, is another hash quickly retrieve data structure, MongoDB HASH type of use shard key hash index.
  • Geospatial Index to support rapid geospatial queries, such as finding nearby businesses 1 km.
  • Text indexing to support fast full-text search
  • Fuzzy Index (Wildcard Index), based on a flexible type matching rule index, introduced since version 4.2.

Evaluation index, Tuning

Use explain () command can be used to query plan analysis, to further assess the effect of the index.
as follows:

> db.test.explain().find( { a : 5 } )

{
  "queryPlanner" : {
    ...
    "winningPlan" : {
      "stage" : "FETCH",
      "inputStage" : {
        "stage" : "IXSCAN",
        "keyPattern" : {
            "a" : 5
        },
        "indexName" : "a_1",
        "isMultiKey" : false,
        "direction" : "forward",
        "indexBounds" : {"a" : ["[5.0, 5.0]"]}
        }
    }},
   ...
}

As can be seen from the results winningPlan execution plan is efficient, such as:

  • The results failed to hit the index will show COLLSCAN
  • The results hit the index, use IXSCAN
  • There has been sort of memory, displayed as SORT

Reference

https://www.cnblogs.com/littleatp/p/11675233.html

Published 351 original articles · won praise 594 · views 370 000 +

Guess you like

Origin blog.csdn.net/BeiisBei/article/details/105211211