官术网_书友最值得收藏!

An introduction to MongoDB

Let's start with a short but informative tour of MongoDB, which will give you the essential knowledge that you need in order to effectively work with it.

First, let's get a good grasp of how data is organized in a MongoDB instance. This will give us the foundation that is required to understand how storage and retrieval operations work later on.

Documents

MongoDB is a NoSQL Database Management System (DBMS). This means that it eschews the traditional table-based data storage model used by SQL-oriented systems such as MySQL, Oracle, and Microsoft SQL Server. Instead, it stores data as documents, which are data structures that are almost identical to standard JSON objects. For example, a MongoDB document can look like this:

{
  "_id" : ObjectId("547cb6f109ce675dbffe0da5"),
  "name" : "Fleur-De-Lys Pharmacy",
  "licenseNumber" : "DL 133",
  "address" : "430, Triq Fleur-de-Lys",
  "geolocation" : {
  "lat" : 35.8938857,
  "lng" : 14.46954679999999
  },
  "postCode" : "BKR 9060",
  "localityId" : ObjectId("54c66564e11825536f510963")
}

This document represents a pharmacy, with some basic information such as the name, address, and national license number. If you are familiar with JSON, you will feel right at home; this is the standard object notation. However, note an unusual datatype in here—the ObjectId. This is a built-in datatype in MongoDB, and it is the default method that is used to uniquely identify a single document. Every single document you store in a MongoDB database is guaranteed to have a unique _id member with respect to that database.

Note

If you are familiar with SQL, you may be tempted to think about it as a column ID. Don't! An _id uniquely identifies a document in the entire database, whereas an SQL column ID only uniquely identifies a row in a table.

Collections

Even though you can uniquely identify a document by its _id, life would be a lot simpler if we could somehow organize documents according to some common characteristics. This is where the concept of a collection comes into play. Simply put, a collection is nothing more than a group of documents that exist in a common folder. For example, we can have a collection named Pharmacies, which will store documents like our preceding example.

If you are used to SQL, you may instinctively feel that the documents in the same collection must somehow have the same structure, just like rows in an SQL table do. Surprisingly, this is not even remotely true. Collections only group documents; they do not impose any structural demands on them (apart from the need to have an _id, but this holds for all the documents and has nothing to do with a particular collection). This means that in the collection that we store our pharmacy-related data in, we may also store documents that describe fruit, people, cars, or movies. Whether we should do so is left entirely up to the programmer. This great freedom of structure is one of the most powerful aspects of MongoDB and a key factor that sets it apart from the more traditional DBMS.

Databases

We now know that MongoDB stores data as documents in collections. The last storage concept that we need to mention is the database itself. Simply put, a database in MongoDB is a top-level organizational structure, which holds a group of collections along with information about users who may access the database, security settings, optimizations, and other configuration options. A single MongoDB instance can manage as many databases as server resources will allow.

Note

It is easy to be misled into thinking that MongoDB itself is the database. Rather, MongoDB is a DBMS, which can manage an arbitrary number of databases.

An example – a product order database

Let's put what we have learned so far into practice and construct a simple MongoDB database that contains data about products, customers, and the orders that the customers have made for specific products. If you are accustomed to other DBMS such as MySQL, you may be surprised to see how simple and intuitive the process is.

主站蜘蛛池模板: 麻江县| 开化县| 潞西市| 芦溪县| 深圳市| 广南县| 通州市| 九寨沟县| 伊宁县| 蒲江县| 仪陇县| 沙湾县| 义马市| 依兰县| 中卫市| 开化县| 弥勒县| 沧州市| 贡山| 鄂托克旗| 邛崃市| 桃源县| 隆回县| 大同市| 旺苍县| 渭南市| 莱西市| 连江县| 锡林浩特市| 河北区| 高唐县| 囊谦县| 松江区| 泾阳县| 偏关县| 资中县| 马边| 台湾省| 岗巴县| 长岛县| 宿迁市|