MongoDB Overview: Getting Started with MongoDB

MongoDB Overview: Getting Started with MongoDB

  • オーケストレーション
6分で読めます 更新日 Oct 24, 2017

ポイント: MongoDBは、柔軟なデータストレージのためにJSONのようなドキュメントを使用するスケーラブルなNoSQLデータベースです。このガイドでは、コマンドラインシェルやドキュメント構造を含む重要な機能をカバーし、ストレージ効率よりもスピードを強調します。

  • データはドキュメントのコレクションに保存され、大規模データ管理が簡素化されます。
  • PythonやJavaを含むさまざまなドライバを通じて複数のプログラミング言語をサポートします。

Here we provide an overview of the MongoDB database. In subsequent posts we will give more in depth examples of how to use MongoDB.

First, MongoDB is a noSQL big data database. It fits the definition of big data, because it scales (i.e., can be made larger) simply by adding more servers to a distributed system. And it is does not require any schema, like an RDBMS database, such as Oracle.

MongoDB data records are stored in JSON (JavaScript Object Notation) format, which is self-describing, meaning the metadata (i.e., schema) is stored together with the data.

Mongo has an interactive command shell. JavaScript programmers will love this because the syntax is JavaScript. To open the shell you simply type:

    mongo

Concepts

MongoDB records are called documents. Each MongoDB database (You can have many.) includes collections, which are a set of JSON documents. Each collection and document has an ObjectID created by MongoDB or supplied by the programmer.

To illustrate, suppose we have one database called products.

We could have two collections to contain all products grouping them by where they are sold:

Collection Documents and fields
Products sold in Europe EAN (European barcode), weight in kilos
Products sold in the USA UPC (American barcode), weight in pounds

Data storage is cheap and memory and CPU costs more. So, some big data databases, like Cassandra and MongoDB, throw out the idea of a normalized database, which is one of the key principles of an RDBMS database.

For example, with Oracle you would have a product category in a product record. The product category table contains fields common to all of those products. Each product record points to a product category record, so that such common data is not stored more than once:

Product table
Fields: product number, product category

 

Product Category table
Fields: product category, weight, color

But then you have to do a join operation if you want to know the color or weight of a product. But a join is a computationally expensive operation. That takes time. MongoDB would store the data like this:

Product documents
Fields: product number, product category, weight, color

RDBMSプログラマーは、重複を作成し、スペースを無駄にすることを言います。MongoDBプログラマーは「はい」と言うでしょうが、スピードはストレージよりも重要です。

言い換えれば、MongoDBのレコードはこのように見えるかもしれません:

Product 1 Category boy’s diapers small Color blue
Product 2 Category boy’s diapers large Color blue
Product 3 Category girl’s diapers small Color pink

明らかに。カテゴリを知っていれば、色を知っているでしょう。

それを示すために、製品データベースを作成し、いくつかの製品を追加します。これらのコマンドをMongoDBシェルに貼り付けてください。

まず、製品データベースを作成します。

    use products
switched to db products

次に、これらの2つのコレクションを:

次に、いくつかのデータを追加します:

    > db.createCollection("boyDiapers")
{ "ok" : 1 }
> db.createCollection("girlDiapers")
{ "ok" : 1 }
>

次に、いくつかのデータを追加します:

    db.boyDiapers.insert([
{
size: 1,
color: 'blue',
brand: 'toddler tyke',
}
])
db.girlDiapers.insert([
{
size: 1,
color: 'pink',
brand: 'little angel',
}
])

2つのことに注意してください。最初に、ドキュメントを追加するためにdb.(collection).insert形式を使用します。次に、ブラケット[]を使用します。これは配列を示し、一度に複数のドキュメントを追加できるようにします。

今、データをクエリできるようにするために、さらにデータを作成します:

    db.boyDiapers.insert([
{
size: 2,
color: 'white',
brand: 'boy large white'
}
])
db.girlDiapers.insert([
{
size: 2,
color: 'while',
brand: 'girl large'
}
])

Selecting Data

If you use find with no arguments it lists all documents. Use pretty to display the results in easy-to-read indented JSON format:

    > db.girlDiapers.find().pretty()
{
"_id" : ObjectId("59d1e9d5ccf50b62c5a7af55"),
"size" : 1,
"color" : "pink",
"brand" : "little angel"
}
{
"_id" : ObjectId("59d1f022ccf50b62c5a7af57"),
"size" : 1,
"color" : "while",
"brand" : "girl large"
}
{
"_id" : ObjectId("59d1f565ccf50b62c5a7af59"),
"size" : 2,
"color" : "while",
"brand" : "girl large"
}

Find all girl diapers of size 2 add arguments to the find statement:

    db.girlDiapers.find({"size":2})
{ "_id" : ObjectId("59d1f565ccf50b62c5a7af59"), "size" : 2, "color" : "while", "brand" : "girl large" }

Now, you could not search both boy’s and girl’s diapers collections at the same time. MongoDB does not do that. Instead you have to program that in your application that you would code using some driver (See below).

Normalized Documents

We just said that in MongoDB there is no normalization because storage is cheap and computational power expensive. But you can create normalize documents.

For example we can create a sales record for each size 2 girl large document like this with the diaper field pointing to the diaper object. That might make more sense in this case as you would not want the diaper collection to grow many times larger each time you make a sale.

    db.girlDiapers.insert([
{ "diaper" : ObjectId("59d1f565ccf50b62c5a7af59"),
"price" : 45.2,
"quanity" : 10,
"sku" : "case"
}
])

Command Line Shell

MongoDB Drivers

Of course, you probably would not use the command line shell for an application. Instead you would write a program to interact with MongoDB using any of the many drivers available. There are drivers for C++, C#, Java, Node.JS, Scala, Python, and more.

For example, to use Python:

    sudo pip install pymongo

Then to query for size 2 diapers across the boy and girl collections:

Outputs:

    from pymongo import MongoClient
client = MongoClient()
db = client.products
x=db.collection_names()
for i in range(len(x)):
c=x[i]
d = db.get_collection(c)
for e in d.find({"size": 2}):
print(e)

Outputs:

    {'size': 2.0, 'brand': 'boy large white', 'color': 'white', '_id': ObjectId('59d1f564ccf50b62c5a7af58')}
{'size': 2.0, 'brand': 'girl large', 'color': 'while', '_id': ObjectId('59d1f565ccf50b62c5a7af59')}

In the next post we will get into some more advanced MongoDB topics.

These postings are my own and do not necessarily represent BMC's position, strategies, or opinion.

オーケストレーションの関連記事

BMCからのさらなる情報に興味がありますか?

ザ・ブリーフは、メインフレームとエンタープライズオーケストレーションに関する月刊メールです:研究、顧客ストーリー、製品ニュース、イベント。

このオファーに登録することで、あなたはBMCのプライバシーポリシーを読み、同意したことを確認します。

ザ・ブリーフに登録しました

ザ・ブリーフは、メインフレームとエンタープライズオーケストレーション—BMC AMIおよびControl-M—に関するアナリストの研究、顧客の展開、両者にわたるイベントを網羅しています。