Understanding the MongoDB Query Language (MQL) data model in the context of Polypheny will enable you to interact more effectively with your data. MQL is based on a document data model, allowing for diverse and flexible structures that can accommodate different types of data with ease.
Documents
Data is organized in documents, which are the basic units of data in MQL. Documents are BSON (Binary JSON) data structures, similar to JSON objects. Each document is made up of field and value pairs, akin to JSON. The values can include other documents, arrays, and arrays of documents.
Here’s an example of a simple document:
{
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"country": "USA"
}
}
This example document contains four fields: name
, email
, age
, and address
. The address
field is itself a document containing its own set of fields.
Collections
Documents are stored in Collections, which are analogous to tables in relational databases. Unlike tables, however, collections do not enforce a uniform structure. This means that different documents within the same collection can contain different fields.
Here’s an example of a collection of documents:
db.users.insertMany([
{ "name": "John Doe", "email": "john@example.com", "age": 30 },
{ "name": "Jane Doe", "email": "jane@example.com", "age": 28, "nickname": "Janey" },
{ "name": "Jim Doe", "email": "jim@example.com", "phone": "123-456-7890" }
])
In this example, the users
collection contains three documents. Notice that not all the documents have the same fields - the second document has an extra nickname
field, and the third document has a phone
field instead of an age
field.
Namespaces
Collections are grouped together to form namespaces. Each namespace has its own set of collections and does not share these collections with other namespaces.
For example, you might have a users
collection and an orders
collection grouped together in a ecommerce
namespace:
use ecommerce
db.users.insert({ "name": "John Doe", "email": "john@example.com", "age": 30 })
db.orders.insert({ "userId": "abc123", "productId": "def456", "quantity": 1 })
In this example, we switch to the ecommerce
namespace and then insert a document into both the users
and orders
collections.
In Polypheny, this flexible and hierarchical data model can be leveraged effectively, especially when dealing with diverse and rapidly evolving data sets.