MongoDB basic concepts

 

Correspondence between MongoDB and relational databases

relational database MongoDB
Database database Database database
Form table Collection collection
Row row Documentation document
Column column Field field
Index index Index index
Table joint table joins                     Embedded documents
Primary key primary key Primary key primary key. MongoDB default primary key for the _id 

 

 

Relational database Example:

id name age score
1 Joe Smith 20 90
2 John Doe 21 95

 

MongoDB corresponding:

{
    "_id":ObjectId(.......),
    "name":"张三",
    "age":20,
    "score":90    
}
{
    "_id":ObjectId(.......),
    "name":"李四",
    "age":21,
    "score":95   
}

 

 

 

 

Database (database)

A MongoDB can create multiple databases, which are independent of each other, and have their own set of permissions. Different databases use a different file storage (not stored in a file).

There are four default MongoDB database:

  • ADMIN : From the perspective of rights, this is "root" database. Adding a user to the database, the user automatically inherits all the permissions database. Some specific server-side command can only be run in the database, such as listing all of the database or server.
  • local:  This database will never be copied, which is any collection of local data (not replicated to other servers MongoDB), can be used to store a single server is limited to a local
  • config : when a slice setting Mongo, config databases use for storing fragmentation related information.

 

Database name to all lowercase.

 

 

 

 

Field (field)

That is a key-value pair, key must be a String, value can be any type.

 

 

Document (document)

 Document is a set of key-value pairs, represented by {}, fields separated by commas. Equivalent to a relational database row (record).

Example: a document

{

  "name":"张三",

  "age":20,

  "score":98

}

See write field for convenience, may be written together { "name": "Joe Smith", "age": 20, "score": 98}, the same.

 

Description:

  • Document key-value pairs are ordered
  • Can not have a document (corresponding to a record in the relational database) duplicate key
  • With "_" at the beginning of the key is reserved, have a special meaning.

 

 

 

Collection (collection)

A collection is a set of documents. Corresponds to a set of relational database tables, but the length of the document set may be different (set of documents may be different for key-number), Key documents in the collection may be different.

Collection is automatically created when the collection to insert the first document.

 

Example: a collection of

{
    "name":"张三",
    "age":20
}
{
    "name":"李四",
    "age":15
}
{    
    "name":"王五",
    "age":20
}

 

 

 

Example: the length of a document set may be different

{
    "name":张三",
    "age":20
}
{
    "name":"李四"
}
{
    "name":"王五",
    "age":20,
    "score":98
}

 

 

 

Example: Different key documents in the collection

{
    "name":"张三",
    "age":20,
    "score":90
}
{
    "domain":"www.baidu.com",
    "ip":"127.23.45.50"
}

 

 

Although the case, but the collection of documents is generally associated.

 

Description:

  • Collection name can not be "system." At the beginning, which is the prefix for the collection system reserved.
  • User-created collection name can not contain reserved characters. 

 

 

 

 

capped collections

Ordinary collection did not specify the size of the space, can store an unlimited number of documents.

capped collection is designated collection space size, the size of this collection is limited, beyond this size, you can not store documents.

the capped, capped, capped, covered, encased.

 

 

And ordinary collection of different, capped collection must be explicitly created, to specify the size of the data storage space when you create.

db.createCollection("mycoll", {capped:true, size:100000})   

The first parameter is the name of a collection, the capped specifies whether a capped collection, size specifies the size of the data storage space capped collection, the default byte units.

 

 

Description:

  • capped collection can be updated, but can not increase the size of the space, if used to increase the size of the space, the update will fail.
  • capped collection can not delete a document, but you can delete all documents using drop () method.

 

 

capped collection characteristics and has a high performance queue expired (the expiration in order of insertion).

capped collection will be high-performance and automatically maintain the insertion order of the document, it is ideal for functions like logging.

capped collection is in accordance with the order of insertion of the document rather than using the index to determine the insertion position, improve the efficiency of the added data.

capped collection is preserved in accordance with the order of insertion of the document to the collection, and these documents stored on the disk location is also in accordance with the insertion order to preserve, so when we update the capped collection in the document, the updated document can not exceed the previous document size, so if you can ensure that all documents on the disk position has remained unchanged.

 

 

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11105425.html