Getting Started with MongoDB (ii): Basic concepts and data types of MongoDB

Source: Internet
Author: User
Tags findone lowercase

On a talk about MongoDB installation and management, which involves a number of concepts, data structure of other API calls, do not know it's okay, in fact very easy, this will introduce.

1. Documentation

Documentation is the core concept of MongoDB. Multiple key-value pairs are ordered together as a document that is the primary data structure for MONGODB to store data.

MongoDB is in the form of documentation, using a JSON-like binary Bson data format, the call to the API is the number of document parameters. Each programming language has a data structure that identifies the document, such as Java's Map,lua Table,python dict and so on, but they are almost identical. A key corresponding to a value. MongoDB's documentation is equivalent to a row of data in a MySQL table relative to MySQL.

Notation 1>db.people.insert ({"Name": "Mary", "Age": 10}) notation 2>person1 = {"Name": "Mary", "Age": 10}>db.people.insert (P Erson1)

The above two formulations are the same. is to insert a document into the People collection of the database.

Just another way of writing is to use a variable person1 to represent the document that you want to insert.

The key-value pairs in the document are ordered, and the following two documents are not the same:

Doc1 = {"Name": "Mary", "age": 10}doc2 = {"Age": Ten, "Name": "Mary"}

The key to a document must be a string type, but the value can be a very many type, as you'll say later.

And the document is differentiated in uppercase and lowercase. The name key and the name key are different. The keys in the document must be unique.

2. Collection

It says people collection. The collection of MongoDB is equivalent to a table of MySQL Table,mongodb list all the collections can be show tables. can also be show collections. A collection is a set of documents and is modeless. The documents in a collection can be of various kinds:

Db.people.insert ({"Name": "Mary", "Age": Ten}) Db.people.insert ({"Country": "USA", "Languge": "中文版"})

If two documents are inserted in the people collection above, the two documents are completely different. Keys are different, such words. That is not all documents can be placed in a set, it is really this, but this is really problematic, when the collection is large to a certain extent, the rate will become very slow, and query efficiency is very low. And it's very complicated in terms of management. So we also want to divide the database into different collections.

The previous article saw a lot of collections. People ah. System.indexes AH. System.users Ah, wait. There are certain restrictions on the naming of collections. First, cannot be empty "", also cannot contain the null character, cannot have the reserved character $,system beginning is even more. This is a prefix that is not reserved for system collections. We just have to use some very simple and easy to name, for our own management and operation is very easy.

The concept of another sub-collection of MongoDB is that a collection consists of several collections, which is also manageable, such as the people of basic information and nationality information on a people collection. Can be written directly into a document, you can also use two sub-sets, a basic information baseinfo, a representation of nationality information countryinfo:

Db.people.baseinfo.insert ({"Name": "Mary", "Age": Ten}) Db.people.countryinfo.insert ("Country": "USA", "Languge": " 中文版 ")

This example is a bit farfetched, but it is easy to understand. As can be seen from the above, we have not created people such a collection can be inserted directly, so for the people who use MySQL is a bit difficult to understand, the table is not created to insert data into it. What's even more outrageous is that none of our databases are created and can be used directly, regardless of database use Anydatabase:

> Show dbsadmin   0.078gbdhh_tp  0.078GBlocal   0.078GBtest    0.078gb> use templeswitched to DB Temple >dbtemple> Show Collections> Db.people.insert ({"Name": "Mary", "Age": Ten}) Writeresult ({"ninserted": 1}) > Show Tablespeoplesystem.indexes> Db.people.find () {"_id": ObjectId ("539137ce90c0386ead60e4f4"), "name": "Mary", " Age ": 10}

Let's see. Show DBS View all the databases. There is no temple, but in the direct use of the time but will not error, with DB to see which database used when it is also really used temple,show collections view all the collection when a collection is not, because this database does not exist. Where did the table come from. You can then insert data directly into the people collection without giving an error. The people collection is generated by the query discovery afterwards. And there's a system.indexes set. The data is also really plugged into the people collection, System.indexes stores the index in the collection, which is later said. MongoDB will check that there is no such a collection of databases when inserting data. If it does not exist, it will be created on its own initiative, instead of creating the collection ourselves, and defining so many fields, the data type of the field. Length and whether it can be empty, unique keys and so on, very convenient and useful.

3. Database

In MongoDB, multiple documents form a collection, and multiple collections form a database, as above people and system.indexes make up the temple database. Each database in MongoDB is independent of each other, has independent permissions, and is stored in different files.

There are certain restrictions on the naming of MongoDB databases, and there can be no spaces or null characters. Also cannot contain special symbols, should all lowercase. Up to 64 bytes, it is very important, the database name will eventually become the file in the system, you can open data/db to see, there will be the same name as your database file. They are the data in the database. The naming of the database should also be common and understandable.

And some names cannot be used. For example, admin. This is the root database without much to say, local. This database cannot be copied, and later said, CONFIG. associated with the Shard. These are the reserved database names.

Start Mongodbserver The previous article has also been introduced. Start mongodbclient The previous article also said. Running MONGO specifies the option to be able to run no matter what JavaScript program is here. It is also possible to perform general operations on the database. Add Insert Delete Remove check find change update and so on, this will be said later.

There are also commands to delete collections and databases:

> Show tablespeoplesystem.indexes> db.people.drop () true> show tablessystem.indexes> use templeswitched to DB temple> dbtemple> db.dropdatabase () {"Dropped": "Temple", "OK": 1}> show dbsadmin   0.078gbdhh_tp  0.0 78GBlocal   0.078GBtest    0.078GB

Db.dropdatabase () Deletes the database used, Db.people.drop () deletes the People collection. These APIs are extremely easy to use.

4. Data type

MongoDB has a rich data type, such as the name field above is a string, and the age field is a number. Of course there are not only these two kinds.

JSON has only 6 data types. NULL, Boolean, number, string, array, and object. MongoDB adds some other data types based on JSON to look at the data types of MongoDB:

null: Used to represent a control or a nonexistent field, such as: {"x": null}.

Boolean : There are only two values of true and false.

32-bit integer : This type is not available in the shell, JavaScript only supports 64-bit floating-point numbers, so 32-bit integers are actively converted by themselves.

64-bit integer : This type is not available in the shell. The shell uses a special inline document to display a 64-bit integer.

64-bit floating point number: The numbers in the shell are this type. {"X": 3.14} and {"X": 3} are floating-point numbers.

Since JavaScript has only one numeric type, it is a 64-bit floating-point type. So the numbers from the shell in MongoDB are treated as 64-bit floating-point, and MongoDB supports three types of numbers, so the shell changes the data in the database will be converted to 64-bit floating-point type.

A 64-bit integer does not accurately represent a 64-bit floating-point type. Assuming that a 64-bit integer is stored in MongoDB, when viewed in the shell, it is assumed that the 64-bit floating-point type can be represented by a key with the built-in document displayed and the value is accurate. Otherwise. He will display a multi-key inline document, indicating that it may not be accurate.

Assuming a 64-bit integer 3, the query display in the shell would look like this:

Db.nums.findOne () {"_id": ObjectId ("4c0beecfd096a2580fe6fa08"), "Myinteger": {"Floatapprox ": 3    }}

Assuming a 64-bit integer 9223372036854775807, the query display in the shell would look like this:

Db.nums.findOne () {        "_id": ObjectId ("4c0beecfd096a2580fe6fa08"),        "Myinteger": {                "Floatapprox": 9223372036854775807,                "top": 2147483647,                "bottom": 4294967295    }}

Top and bottom represent high 32-bit and low 32-bit respectively.

string : The UTF-8 string can be represented as a string type of data, such as: {"name": "Mary"}.

symbol : The shell does not support such a type, and the shell converts the symbol type in the database into a string.

Object ID: The object ID is a unique 12-byte ID for the document.

The document stored in MongoDB must have a key "_id". This key can be arbitrary type, by default is the Objectid object, when we save the document without specifying the key, then MongoDB will voluntarily join such a key value pair, this value is unique identification, Objectid use 12 bytes of storage space.

Date : The date type stores the number of milliseconds starting from the standard era and does not store the time zone, such as {"X": New Date ()}.

A Date object in JavaScript that is used as a day type for MongoDB. The Create Date object is returned with a new date () instead of date (), and the string representation of the date is not the true Date object.

This is a statement : The document can include a regular form. Use JavaScript's regular table-like syntax, such as: {"X":/foobar/i}.

Code : The document can also include JavaScript code, such as: {"X": function () {/*...*/}}.

Binary data : Binary data can be made up of random bytes of string, but cannot be used in the shell.

Maximum value : Bson contains a special type that represents the maximum possible value, which is not in the shell.

Minimum value : Bson contains a special type. Represents the minimum possible value. There is no such type in the shell.

no definition : The document can also be used without a defined type, such as: {"X": undefined}

Array : A collection of values, or a list that can represent arrays. The elements in the array can be different types of data, such as {"X": ["a", "B", "C", 20]}.

Inline Documents : Documents can include other documents and can be embedded as values in the parent document. For example: {"X": {"foo": "Bar"}}.

In the case I'm using now, Boolean, number, string, date. Arrays and inline documents are the most used.

Getting Started with MongoDB (ii): Basic concepts and data types of MongoDB

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.