3. Your First JCR Query

Welcome to the exciting part of our Magnolia JCR tutorial - creating and executing your very first query. This step is crucial to understand how to interact with JCR in Magnolia. By the end of this section, you will have a good understanding of how to formulate basic queries. Let's go!

A JCR query in SQL2 syntax typically follows this basic structure:

SELECT [what to select] FROM [node type] WHERE [condition]

This query does the following:

  • SELECT Clause
    Specifies what you want to retrieve. It can be a specific property (propertyName), all properties (*), or a combination of properties.
  • FROM Clause
    Specifies the node type you're querying. In Magnolia, common node types include mgnl:page, mgnl:content, etc.
  • WHERE Clause
    Specifies conditions to filter the nodes. This can include path conditions, property values, etc.

Let's write a basic query to select all the top-level pages of your website:

SELECT * FROM [mgnl:page] WHERE ISDESCENDANTNODE('/website')

This query does the following:

  • SELECT *
    This selects all properties of the nodes.
  • FROM [mgnl:page]
    You're querying nodes of type mgnl:page, which typically represent pages in Magnolia.
  • WHERE ISDESCENDANTNODE('/website')
    This filters the results so that they only contain pages under the path "/website", which in this case is the root directory for the website content. Your Magnolia installation may of course have a different root path.
  1. Enter the Query
    Type or paste your query into the Query Input Area.

  2. Execute the Query
    Click on the 'Execute' or 'Run' button.

  3. View the Results
    The Results Display area will show the pages that match your query. If you've just started with Magnolia, there might be only a few pages or even none, depending on your setup.

 

Example Query

Let's put this together in an more complex example query:

SELECT title, created FROM [mgnl:page] 
WHERE ISDESCENDANTNODE('/website') AND author = 'Max Muster' 
ORDER BY created DESC

This query does the following:

  • Selects the title and created date of pages.
  • From nodes of type mgnl:page.
  • That are under the '/website' path.
  • Where the author property is 'Max Muster'.
  • Orders the results by creation date in descending order