Basic Queries using MQL

Understanding how to construct basic queries in the MongoDB Query Language (MQL) is a fundamental skill when using Polypheny. This page offers an overview of basic CRUD (Create, Read, Update, and Delete) operations, the syntax and structure of JSON documents, and more.

CRUD Operations

CRUD operations are the basic interactions you can perform on your data. They take the form of various methods that you can call on your collections.

Create

Creating data involves inserting documents into collections. The insertOne() and insertMany() methods are used to insert one or many documents respectively. Here’s an example:

db.collection.insertOne({ "name" : "John Doe", "email" : "john@example.com" })
db.collection.insertMany([{ "name" : "Jane Doe", "email" : "jane@example.com" }, { "name" : "Jim Doe", "email" : "jim@example.com" }])

Read

Reading data can be done using the find() method. It can take two arguments: the query criteria and the projection. The projection specifies which fields to include or exclude in the returned documents.

Here’s an example where we find all documents with a field age greater than 30, but only return the name field:

db.collection.find({ "age" : { $gt : 30 } }, { "name" : 1 })

Update

Updating data involves using updateOne(), updateMany(), or replaceOne() methods. Here’s an example of updating:

db.collection.updateOne({ "name" : "John Doe" }, { $set : { "email" : "john@newdomain.com" } })

This operation finds the first document where the name is “John Doe” and sets the email to “john@newdomain.com”.

Delete

Deleting data can be done using the deleteOne() or deleteMany() methods. Here’s an example of deletion:

db.collection.deleteOne({ "name" : "John Doe" })

This operation deletes the first document it finds where the name is “John Doe”.

JSON Documents: Syntax and Structure

MQL is based on the document data model, where data is stored as JSON-like documents. The syntax and structure for a JSON document look like this:

{
  "fieldName1" : "value1",
  "fieldName2" : {
    "subFieldName1" : "subValue1",
    "subFieldName2" : "subValue2"
  },
  "fieldName3" : ["value3a", "value3b", "value3c"]
}

Selecting Fields

When performing a read operation, you can specify which fields to return using projection. The following query returns only the name field for each document where the age is greater than 30:

db.collection.find({ "age" : { $gt : 30 } }, { "name" : 1 })

Limit and Skip Results

Limit and skip can be used to control the number of documents returned and to skip over a number of documents. The following example returns the first 5 documents after skipping the first 10:

db.collection.find().skip(10).limit(5)

Counting and Sorting Documents

The count() method is used to count the number of documents that match a query, and the sort() method is used to sort the documents in

a particular order. Here’s an example:

db.collection.find({ "age" : { $gt : 30 } }).count()
db.collection.find().sort({ "name" : 1 })

The first operation counts the number of documents where the age is greater than 30. The second operation returns all documents sorted by the name field in ascending order (1 means ascending and -1 means descending).

© Polypheny GmbH. All Rights Reserved.