Di tutorial ini, saya mengasumsikan anda sudah menginstall MongoDB server dan sudah menjalankannya. perintah ini hanyalah perintah dasar dari MongoDB mengingat query di MongoDB bisa sangat komplek.Jika anda belum menginstall mongodb di komputer, silahkan langsung saja buka try.mongodb.org> Untuk tutorial ini, ceritanya kita akan membuat database kampus dan tabel mahasiswa. Show
Membuat dan menghapus databasemembuat database use kampus; Menghapus database db.dropDatabase(); Membuat dan tabel Mahasiswamembuat tabel mahasiswa use kampus; db.createCollections("mahasiswa"); Menghapus tabel mahasiswa db.mahasiswa.drop(); Perintah CRUDbuat kembali database kampus dan tabel/collection mahasiswa use kampus; db.createCollections("mahasiswa"); Insert data > db.mahasiswa.insert( { nama:"Candra adi putra", ipk:3.3, jurusan:"TI" } ) > db.mahasiswa.insert({nama:"Adi Nur Wibowo"}); > db.mahasiswa.insert({nama:"Sundari",jurusan:"Ekonomi"}); Dalam contoh diatas, saya menginsert 3 data. Perhatikan bahwa tidak ada istilah kolom dalam database MongoDB. Anda bebas menginsert data, entah cuma nama, nama dan jurusan atau nama, jurusan dan ipk. Jika anda butuh kolom tambahan, tidak ada perubahan schema didatabase, cukup insert dengan pola diatas. Sekali lagi, MongoDB bersifat Dynamic Schema, tidak ada namanya db.dropDatabase();2atau db.dropDatabase();3. Melihat data > db.mahasiswa.find() { "_id" : ObjectId("53130eaa999a7b243bad3b59"), "nama" : "Candra Adi putra", " k" : 3.3 } { "_id" : ObjectId("53130eca999a7b243bad3b5a"), "nama" : "Adi Nur Wibowo" } { "_id" : ObjectId("53130ee0999a7b243bad3b5b"), "nama" : "Sundari", "jurusan" "Ekonomi" } > db.mahasiswa.find().pretty() { "_id" : ObjectId("53130eaa999a7b243bad3b59"), "nama" : "Candra Adi putra", "ipk" : 3.3 } { "_id" : ObjectId("53130eca999a7b243bad3b5a"), "nama" : "Adi Nur Wibowo" } { "_id" : ObjectId("53130ee0999a7b243bad3b5b"), "nama" : "Sundari", "jurusan" : "Ekonomi" } Perhatikan field _id, ini adalah primar key di tabel mongoDB. Primarykey selalu menggunakan kolom _id dan isinya dibuat secara otomatis, user tidak bisa membuat primary key sendiri. db.mahasiswa.find({},{"nama":1}) "_id" : ObjectId("53130eaa999a7b243bad3b59"), "nama" : "Candra Adi putra" } "_id" : ObjectId("53130eca999a7b243bad3b5a"), "nama" : "Adi Nur Wibowo" } "_id" : ObjectId("53130ee0999a7b243bad3b5b"), "nama" : "Sundari" } Menyembunyikan kolom _id dari hasil > db.mahasiswa.find({},{"nama":1,_id:0}) { "nama" : "Candra Adi putra" } { "nama" : "Adi Nur Wibowo" } { "nama" : "Sundari" } Pencarian data > db.mahasiswa.find({nama:"Sundari"}).pretty(); { "_id" : ObjectId("53130ee0999a7b243bad3b5b"), "nama" : "Sundari", "jurusan" : "Ekonomi" } Mengupdate data db.dropDatabase();0 Menghapus data db.dropDatabase();1 Tutorial yang lebih lengkap bisa anda baca di http://www.tutorialspoint.com/mongodb/index.htm Setelah belajar tutorial dasar MongoDB, terasa banget kan syntaxnya agak ribet. Bagi yang terbiasa dengan MySQL juga harus belajar lagi karena sytle syntaxnya beda 100%. Terus ada tidak cara yang lebih mudah untuk mengelola data, misal yang berbasis GUI? Ada dong. Untuk tutorialnya akan saya bahas di artikel selanjutnya. This tutorial also assumes that a MongoDB instance is running on the default host and port. Assuming you have downloaded and installed MongoDB, you can start it like so: $ mongod Making a Connection with MongoClientThe first step when working with PyMongo is to create a to the running mongod instance. Doing so is easy: >>> from pymongo import MongoClient >>> client = MongoClient() The above code will connect on the default host and port. We can also specify the host and port explicitly, as follows: >>> client = MongoClient('localhost', 27017) Or use the MongoDB URI format: >>> client = MongoClient('mongodb://localhost:27017/') Getting a DatabaseA single instance of MongoDB can support multiple independent databases. When working with PyMongo you access databases using attribute style access on instances: >>> db = client.test_database If your database name is such that using attribute style access won’t work (like >>> from pymongo import MongoClient >>> client = MongoClient()9), you can use dictionary style access instead: >>> db = client['test-database'] Getting a CollectionA collection is a group of documents stored in MongoDB, and can be thought of as roughly the equivalent of a table in a relational database. Getting a collection in PyMongo works the same as getting a database: >>> collection = db.test_collection or (using dictionary style access): >>> collection = db['test-collection'] An important note about collections (and databases) in MongoDB is that they are created lazily - none of the above commands have actually performed any operations on the MongoDB server. Collections and databases are created when the first document is inserted into them. DocumentsData in MongoDB is represented (and stored) using JSON-style documents. In PyMongo we use dictionaries to represent documents. As an example, the following dictionary might be used to represent a blog post: >>> import datetime >>> post = {"author": "Mike", ... "text": "My first blog post!", ... "tags": ["mongodb", "python", "pymongo"], ... "date": datetime.datetime.utcnow()} Note that documents can contain native Python types (like instances) which will be automatically converted to and from the appropriate BSON types. Inserting a DocumentTo insert a document into a collection we can use the method: $ mongod0 When a document is inserted a special key, >>> client = MongoClient('localhost', 27017)2, is automatically added if the document doesn’t already contain an >>> client = MongoClient('localhost', 27017)2 key. The value of >>> client = MongoClient('localhost', 27017)2 must be unique across the collection. returns an instance of . For more information on >>> client = MongoClient('localhost', 27017)2, see the documentation on _id. After inserting the first document, the posts collection has actually been created on the server. We can verify this by listing all of the collections in our database: $ mongod1 Getting a Single Document WithThe most basic type of query that can be performed in MongoDB is . This method returns a single document matching a query (or >>> client = MongoClient('mongodb://localhost:27017/')0 if there are no matches). It is useful when you know there is only one matching document, or are only interested in the first match. Here we use to get the first document from the posts collection: $ mongod2 The result is a dictionary matching the one that we inserted previously. Note The returned document contains an >>> client = MongoClient('localhost', 27017)2, which was automatically added on insert. also supports querying on specific elements that the resulting document must match. To limit our results to a document with author “Mike” we do: $ mongod3 If we try with a different author, like “Eliot”, we’ll get no result: $ mongod4 Querying By ObjectIdWe can also find a post by its >>> client = MongoClient('mongodb://localhost:27017/')4, which in our example is an ObjectId: $ mongod5 Note that an ObjectId is not the same as its string representation: $ mongod6 A common task in web applications is to get an ObjectId from the request URL and find the matching document. It’s necessary in this case to convert the ObjectId from a string before passing it to >>> client = MongoClient('mongodb://localhost:27017/')5: $ mongod7 See also Bulk InsertsIn order to make querying a little more interesting, let’s insert a few more documents. In addition to inserting a single document, we can also perform bulk insert operations, by passing a list as the first argument to . This will insert each document in the list, sending only a single command to the server: $ mongod8 There are a couple of interesting things to note about this example:
Querying for More Than One DocumentTo get more than a single document as the result of a query we use the method. returns a instance, which allows us to iterate over all matching documents. For example, we can iterate over every document in the >>> db = client.test_database5 collection: $ mongod9 Just like we did with , we can pass a document to to limit the returned results. Here, we get only those documents whose author is “Mike”: >>> from pymongo import MongoClient >>> client = MongoClient()0 CountingIf we just want to know how many documents match a query we can perform a operation instead of a full query. We can get a count of all of the documents in a collection: >>> from pymongo import MongoClient >>> client = MongoClient()1 or just of those documents that match a specific query: >>> from pymongo import MongoClient >>> client = MongoClient()2 Range QueriesMongoDB supports many different types of advanced queries. As an example, lets perform a query where we limit results to posts older than a certain date, but also sort the results by author: >>> from pymongo import MongoClient >>> client = MongoClient()3 Here we use the special >>> db = client.test_database9 operator to do a range query, and also call to sort the results by author. IndexingAdding indexes can help accelerate certain queries and can also add additional functionality to querying and storing documents. In this example, we’ll demonstrate how to create a unique index on a key that rejects documents whose value for that key already exists in the index. First, we’ll need to create the index: >>> from pymongo import MongoClient >>> client = MongoClient()4 Notice that we have two indexes now: one is the index on >>> client = MongoClient('mongodb://localhost:27017/')4 that MongoDB creates automatically, and the other is the index on >>> db = client['test-database']2 we just created. Mengapa harus menggunakan MongoDB?MongoDB semakin banyak digunakan oleh developer karena berbagai keunggulan yang dimilikinya, yaitu: Performa lebih cepat. Pengelolaan database lebih mudah. Mampu menampung banyak data bervariasi.
Kapan harus menggunakan MongoDB?Kapan Menggunakan MongoDB
Misalnya kamu memiliki toko online yang ramai pelanggan yang setiap menitnya terdapat ada 100 data pelanggan yang masuk. Selain itu kamu juga bisa menggunakan MongoDB saat data yang didapatkan berkembang secara cepat dan memiliki struktur data yang kompleks.
MongoDB menggunakan bahasa apa?MongoDB sendiri ditulis dengan bahasa C++ dan telah tersedia untuk berbagai jenis bahasa pemrograman. Fitur utama dari mongoDB antara lain: model document-oriented storage.
Apa itu Collection dalam MongoDB?Collection MongoDb adalah tempat kumpulan informasi data yang berbentuk dokumen. Collection dipadankan seperti tabel-tabel yang berisi data pada database SQL. Document MongoDb adalah satuan unit terkecil dalam MongoDB.
|