# Write MQL with Gemini assistance

> [!WARNING]
>
> **Preview
> --- [Gemini](https://docs.cloud.google.com/gemini/docs/overview.md.txt) in Firestore**
>
>
> This feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the
> [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

This document describes how you can get AI-powered
assistance in Firestore to generate
MongoDB-compatible Query Language (MQL) queries using natural language
prompts.

Learn [how and when Gemini
for Google Cloud uses your data](https://docs.cloud.google.com/gemini/docs/discover/data-governance).

## Before you begin

1. Optional: [Set up Gemini Code Assist](https://docs.cloud.google.com//gemini/docs/discover/set-up-gemini).

2. To complete the tasks in this document, ensure that you have the
   [necessary Identity and Access Management (IAM) permissions](https://docs.cloud.google.com/firestore/mongodb-compatibility/docs/write-mql-gemini.md.txt#required-roles).

### Required roles


To get the permissions that
you need to complete the tasks in this document,

ask your administrator to grant you the
[Gemini for Google Cloud User](https://docs.cloud.google.com/iam/docs/roles-permissions/cloudaicompanion.md.txt#cloudaicompanion.user) (`roles/cloudaicompanion.user`) IAM role on the project.


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

## Generate MQL queries using natural language prompts

> [!CAUTION]
> As an early-stage technology, Gemini for Google Cloud
> products can generate output that seems plausible but is factually incorrect. We recommend that you
> validate all output from Gemini for Google Cloud products before you use it.
> For more information, see
> [Gemini for Google Cloud and responsible AI](https://docs.cloud.google.com/gemini/docs/discover/responsible-ai.md.txt).

You can give Gemini natural language comments (or *prompts*) to
generate queries that are based on your schema. For example, you can prompt
Gemini to generate MQL in response to the following prompts:

- "How many popular books with publication year 1960?"
- "Create a sample collection of popular books."

> [!NOTE]
> **Note:** When you enter a prompt for Gemini in Firestore, the database runs a `$sample` operation to retrieve 10 random documents to provide information about the database schema. You are charged for the read units for this operation.

To generate MQL in Firestore with Gemini
assistance, follow these steps:

1. In the Google Cloud console, go to the Firestore **Databases** page.

   [Go to Databases](https://console.cloud.google.com/firestore/databases)
2. Select an Firestore with MongoDB compatibility database from the list. The **Firestore Studio** opens.

3. In a new or empty query editor, click the **Generate MQL** button. Otherwise,
   click **Help me code**.

4. Enter a prompt to use to generate a query. To improve accuracy,
   select a collection for context in the drop-down.

5. Review the generated MQL and take any of the following actions:

   - To accept MQL generated by Gemini, click **Insert** . You can continue to edit the MQL in the editor. Click **Run** to run you query.
   - To edit your prompt, click **Edit**.

## What's next

- Read [Gemini for Google Cloud overview](https://docs.cloud.google.com//gemini/docs/overview).
- Learn [how Gemini uses your
  data](https://docs.cloud.google.com//gemini/docs/discover/data-governance).