Cara menggunakan mongodb constraints

Sebagai pegiat IT mungkin kita sudah mengenal atau setidaknya mendengar istilah MySQL. Yaps, Sebuah database relasional populer yang mengusung konsep relasi tradisonal (RDBMS/SQL). Nah, selain RDBMS/SQL juga ada konsep baru yaitu NoSQL. NoSQL merupakan konsep penyimpanan database dinamis yang tidak terikat pada relasi-relasi tabel yang kaku seperti RDBMS. Selain lebih scalable, NoSQL juga memiliki performa pengaksesan yang lebih cepat. Hal-hal itulah yang membuat NoSQL menjadi semakin populer beberapa tahun belakangan.

Salah satu jenis NoSQL yang banyak digunakan dan bersumber terbuka yaitu MongoDB. Dengan sistem penyimpanan dalam format dokumen Binary JSON (BSON) membuat MongoDB sangat cocok untuk dipasangkan dengan Node.js.

Akan menjadi tantangan yang menarik, jika kita selama ini terbiasa dengan konsep SQL dan ingin memahami konsep NoSQL pada MongoDB. Dibutuhkan cara pandang baru, mengingat dua konsep ini memiliki cara berbeda dalam merepresentasikan data.

Mapping Tabel, Kolom dan Baris

Jika pada RDBMS kita mengenal tabel, maka di mongoDB ada istilah collection. Di dalam collection ada bentuk dokumen-dokumen yang sepadan dengan baris pada tabel. Dan nama-nama kolom pada tabel bisa dijumpai sebagai field di MongoDB.

Cara menggunakan mongodb constraints
  • In the Query Designer, click the Query Builder button to open the visual SQL Builder.

    The database objects are displayed in left pane, whereas the right pane is divided into two portions: the upper Diagram Design pane, and the lower Syntax pane:

    Cara menggunakan mongodb constraints
  • It's a good idea to select the tables first, so that the Query Builder knows which fields to present for the field list:

    • Drag a table/view from the left pane to the Diagram Design pane or double-click it to add it to query. We'll be needing the actor, film_actor, and film tables.
    • You can assign table aliases by clicking on "" beside each table. To add the table alias, simply double-click the table name and enter the alias in the Diagram Design pane.
    • Note how the Query Builder already knows the table relationships. That's because foreign key constraints have already been declared on Table objects:

      Cara menggunakan mongodb constraints
    • To include a field in the query, check the left of the field name in the Diagram Design pane. To include all the fields, click at the left of the object caption. Select the actor first and last names as well as the film title.

    Adding WHERE Criteria

    Clicking on "" beside the WHERE keyword adds a default WHERE condition of "<--> = <-->".

    • Click on the left-hand "<--> = <-->" to select a field. That opens a popup dialog that contains a List of fields as well as an Edit tab.
    • Click the List tab and choose the f.release_year field.
    • Click OK to close the dialog.
    • Next, click on the right-hand "<--> = <-->" to set the release year. This time enter a value of "2006" in the Edit tab. Click OK to close the dialog.
    • Click OK to close the Query Builder. You should now see the generated SELECT statement in the Query Editor:
    • SELECT
      a.first_name,
      a.last_name,
      f.title
      FROM
      actor AS a
      INNER JOIN film_actor AS fa ON fa.actor_id = a.actor_id
      INNER JOIN film AS f ON fa.film_id = f.film_id
      WHERE
      f.release_year = 2006
      	
    • Click the Run button to execute the query. The results will be sorted by Film title:
    • Cara menggunakan mongodb constraints

    Whether you're a novice or experience DBA, Navicat's Query Builder makes writing SELECT queries easier than ever before. In an upcoming blog, we'll get into some of its more advanced features.