MongoDB (2) Basic Concepts & Document reference

Source: Internet
Author: User
Tags data structures mongodb mongodb relationships regular expression reserved
MongoDB Getting Started column

Http://blog.csdn.net/column/details/19681.html


MongoDB Basic Concept
The following is a comparison of some of the concepts of SQL terminology and MongoDB terminology:

SQL Terminology/Concepts MongoDB Terminology/Concepts Explanation/Description
Database Database Database
Table Collection Database Tables/Collections
Row Document Data record lines/documents
Column Field Data fields/Fields
Index Index Index
Table joins Table connection, MongoDB not supported
Primary key Primary key Primary key, MongoDB automatically sets the _id field as the primary key


DatabaseA mongodb can establish multiple databases, the default database is "DB", the database is stored in the startup parameters dbpath the specified directory; a single instance of MongoDB can hold multiple independent databases database, each of which has its own set of Col Lection and permissions, different databases are also placed in different files;
Here are some basic instructions for viewing the database:
# Show all databases under current permissions for MongoDB
# Displays the current database object or collection name
Db
# Connect (use) the specified database
Use Data_name
MongoDB has the following reserved database, Admin: From a permission point of view, this is the "root" database. If you add a user to this database, the user automatically inherits permissions from all databases. Some specific server-side commands can only be run from this database, such as listing all databases or shutting down the server;
Local: This data is never copied and can be used to store any collection that is limited to a local single server;
Config: When MONGO is used for sharding settings, the Config database is used internally to hold information about the Shard;

Set ColletionCollection is a document group of MongoDB, similar to a tabular table in an RDBMS, a collection exists in a database, and a collection has no fixed structure, that is, the collection can be inserted into different formats and types of data, but usually the data inserted into the collection will have some relevance. If you can insert a document of the following different data structures into the document:
{"Site": "Www.baidu.com"}
{"Site": "www.google.com", "name": "Google"}
Here are the instructions for viewing collection:
Use local
Show Collections # shows all collection under the current DB
Show Tables # ditto
For the naming of collection, it is not possible to start with "system." And it is best not to have the reserved characters;
Document DocumentsDocument is a set of key-value (Key-value) pairs (that is, Bson), MONGO documents do not need to set the same fields, and the same fields do not require the same data type; A simple document example is as follows:
{"Site": "www.google.com", "name": "Google"}
The value in document can be a variety of data types supported by MongoDB, distinguishing between type and case, the same key cannot exist in a document, and the document key is of type string (typically UTF-8 encoded format);
Meta DataThe meta-data (meta-information) of each database in MongoDB is stored in collection under <db_name>.system.*, the main colletion as follows:
Dbname.system.namespaces List all namespaces
Dbname.system.indexes List all indexes
Dbname.system.profile Contains database profile information
Dbname.system.users List all users who can access the database
Dbname.local.sources Server information and status that contains replication peer-to-peer (slave)

The following restrictions apply to modifying objects in a System collection. {system.indexes} inserts data, you can create an index, but otherwise the table information is immutable; {system.users} is modifiable; {system.profile} is removable;

Data Type

MongoDB supports a variety of data types, the following are several common;

String String. Data types commonly used to store data. In MongoDB, the UTF-8 encoded string is legal.
Integer Integer value. Used to store numeric values. Depending on the server on which the environment resides, it can be divided into 32-bit or 64-bit.
Boolean Boolean value. Used to store Boolean values (True/false).
Double Double-precision floating-point value. Used to store floating-point values.
Min/max keys Compares a value to the lowest and highest value of a BSON (binary JSON) element.
Array Used to store an array or list or multiple values as a single key.
Timestamp Time stamp. Record when the document was modified or added.
Object Used for inline documents.
Null Used to create a null value.
Symbol Symbol. The data type is basically the same as the string type, but unlike it, it is typically used in languages with special symbol types.
Date Date time. Use the UNIX time format to store the current date or time. You can specify your own datetime: Create a Date object and pass in the month-date information.
Object ID The object ID. The ID used to create the document.
Binary Data Binary data. Used to store binary data.
Code The code type. Used to store JavaScript code in a document.
Regular expression The regular expression type. Used to store regular expressions.


MongoDB ObjectIdMongoDB's ObjectId is used to represent the _ID key of the document, usually auto-generated, ObjectId is a 12-byte BSON type data in the following format:
_id:objectid ("5a83c0d8a04c12209d79eea1")
< timestamp-4 bytes >< Machine identification code-3 bytes >< process IP (PID)-2 bytes >< random number-3 bytes >
Create a new Objectid
> Newobjectid = ObjectId ()
# You can use the ObjectId you create instead of the ObjectId created automatically by the system
> Myobjectid = ObjectId ("5349b4ddd2781d08c09890f4")
gets the timestamp of the document
>objectid ("5349b4ddd2781d08c09890f4"). Gettimestamp ()
Isodate ("2014-04-12t21:49:17z")
ObjectId Convert to String
> New ObjectId (). Str
5a85711e049466e633bc36ba


MongoDB's relationshipMongoDB relationships represent the logical interrelationship between multiple documents, which can be established through embedding and referencing. The relationships in MongoDB include the following: 1:1
1:n
N:1
N.
Here are 2 simple document structures: UserDocument
{
"_id": ObjectId ("52ffc33cd85242f436000001"),
"Name": "Tom Hanks",
"Contact": "987654321",
}
AddressDocument
{
"_id": ObjectId ("52ffc4a5d85242602e000000"),
"Building": "A, Indiana Apt",
"Pincode": 123456,
"City": "Los Angeles",
"State": "California"
The relationship between user and address is 1:n and can be expressed in the following 2 ways:
Embedded RelationshipsAddress can be embedded in the user document type, such advantages are more intuitive, easy to find, but with the increase in data volume, will greatly affect the reading and writing speed;
{
"_id": ObjectId ("52ffc33cd85242f436000001"),
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.