On a talk about MongoDB installation and management, which involves a number of concepts, data structure and some API calls, do not know it's okay, actually very simple, this will be a brief introduction.
1. Documentation
The document is the core concept of MongoDB, and multiple key-value pairs are placed together as a document, and the document is the most basic data structure for MONGODB to store data. MongoDB is in the form of documentation, using a JSON-like binary Bson data format, the API calls are passed 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 it is all the same, and a key corresponds 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)
Both of the above are the same, inserting a document into the People collection of the database. Just the second notation uses a variable person1 to represent the document to be inserted.
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 the document must be a string type, but the value can be a lot of types, as you'll say later. And the document is case-sensitive, the name key and the name key are different. The keys in the document must be unique.
2. Collection
Above the People collection, the collection of MongoDB is equivalent to a table of MySQL Table,mongodb list all the collections can be show tables, or show collections. A collection is a set of documents, and is modeless, and the documents in a collection can be of various kinds:
Db.people.insert ({"Name": "Mary", "Age": Ten}) Db.people.insert ({"Country": "USA", "Languge": "中文版"})
As above two documents are inserted into the people collection, but the two documents are completely different, the keys are not the same, so that not all the documents can be placed in a collection, it is true, but this is really problematic, when the collection is large to a certain extent, the rate will become very slow, And the query efficiency is very low, and the management aspect is very complex, so we also want to divide the database into different sets.
The previous article saw a lot of collections, people ah, system.indexes Ah, system.users ah, and so on, the collection of naming also has a certain limit, the first can not be empty "", can not contain empty characters, can not have reserved characters $,system beginning of the more dead, This is a prefix that is not reserved for system collections. We just need to use some very easy to understand the name of the good, for our own management and operation is very simple.
MongoDB also has a sub-collection concept, that is, a collection contains several sets, which is also easy to manage, such as the above will people basic information and nationality information in a people set of a bit reluctant to write directly into a document, you can also use two sub-sets, A representation of the 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's easy to understand. As can be seen from the above, we have not created a people such a collection can be inserted directly, so for the use of MySQL is a bit difficult to understand, the table is not created to insert data to it, the more outrageous is that our database is not created, you can directly use any database using the 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}
See, Show DBS View all the database, there is no temple, but in the direct use of the time without error, in DB to see which database is used when it is also really used Temple,show Collections view all collections when a collection is not there, because this database does not exist, which comes from the table. And then directly to the people collection to insert the data will not be an error, after the query found that the People collection was generated, And there is a System.indexes collection, and the data is actually plugged into the people collection, System.indexes stores the index in the collection, which is later said. MongoDB will be when inserting data to check if there is such a database such a collection, if it does not exist automatically created, we do not have to create a collection, but also to define so many fields, field data type, length and can be empty, unique key and so on, very convenient and practical.
3. Database
In MongoDB, multiple documents form a collection, and multiple collections form a database, such as the above people and system.indexes, which make up the temple database. Each database in MongoDB is independent of each other, has separate permissions, and is stored in different files.
MongoDB database naming also has a certain limit, can not have spaces or empty characters, can not include special symbols, should all lowercase, up to 64 bytes, it is important that the database name will eventually become the system files, you can open data/db to see, There will be the same name as your database file, which is the database data, database naming should also be common understandable. And can not use some names, such as admin, this is the root database does not have to say, local, this database can not be copied, later said, config, and shard related, these are reserved database name.
The start of the MongoDB server is also described in the previous article. Start MongoDB Client on the previous article also said that the execution of MONGO specified options, where you can execute any JavaScript program, you can also perform general operations on the database, add Insert Delete to find the 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 easy to use.
4. Data type
MongoDB has a very rich data type, such as the name field above is a string, the age field is a number, of course, not only these two. JSON has only 6 data types, NULL, Boolean, numeric, string, array, and object. MongoDB adds some other data types based on JSON, and here's a look at the data types of MongoDB:
null: Used to represent a control or a nonexistent field, such as: {"x": null}.
Boolean : Only two values are 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 automatically converted.
64-bit integer : This type is not available in the shell, and the shell uses a special inline document to display a 64-bit integer.
64-bit floating-point numbers: The number in the shell is this type, {"X": 3.14} and {"X": 3} are all floating points.
Since JavaScript has only one numeric type that is 64-bit floating point, the numbers from the shell in MongoDB are treated as 64-bit floating-point types, while MongoDB supports three number type, so the shell modifies the data in the database to be converted to 64-bit floating-point. A 64-bit integer does not accurately represent a 64-bit floating point, and if a 64-bit integer is stored in MongoDB, when viewed in the shell, if it can represent a 64-bit floating-point type then the built-in document with a key is displayed and the value is accurate, otherwise, he displays a multi-key embedded document that may be inaccurate.
If it is a 64-bit integer 3, then the query display in the shell would look like this:
Db.nums.findOne () {"_id": ObjectId ("4c0beecfd096a2580fe6fa08"), "Myinteger": {"Floatapprox ": 3 }}
If it is a 64-bit integer 9223372036854775807, then 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 this 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 any type, the default is the Objectid object, when we save the document without specifying the key, then MongoDB will automatically add such a key value pair, this value is a unique identifier, Objectid uses 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 ()}.
In JavaScript, the Date object is used as the type of the MongoDB, and the creation date object is returned with the new date () instead of date (), and the string representation of the date, not the true Date object.
Regular Expressions : The document can contain regular expressions, using JavaScript's regular expression syntax, such as: {"X":/foobar/i}.
Code : The document can also contain JavaScript code, such as: {"X": function () {/*...*/}}.
Binary data : Binary data can consist of strings of any byte, but cannot be used in the shell.
Maximum value : Bson includes a special type that represents the maximum possible value, which does not have this type in the shell.
Minimum value : Bson includes a special type that represents the minimum possible value, which is not in the shell.
undefined: An undefined type can also be used in a document, such as: {"X": undefined}
Array : A collection of values or a list that can represent an array of elements that can be of different types of data, such as {"X": ["a", "B", "C", 20]}.
Inline Document : A document can contain other documents, or it can be embedded as a value in a parent document, such as {"X": {"foo": "Bar"}}.
In my current use case, Boolean, number, string, date, array and inline documents are the most used.