Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products.

Using Cloud Firestore in Datastore Mode

Stay organized with collections Save and categorize content based on your preferences.

Firestore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. It is the newest version of Datastore and introduces several improvements over Datastore.

Because Firestore in Datastore mode is optimized for server use cases and for App Engine, we recommend using Firestore in Datastore mode for databases that will be used primarily by App Engine apps. Firestore in Native mode is most useful for mobile and real-time notification use cases. For more information about Firestore modes, see Choosing between Native Mode and Datastore mode.

This document describes how to use the Google Cloud Client Library to store and retrieve data in a Datastore mode database.

Prerequisites and setup

Follow the instructions in "Hello, World!" for PHP on App Engine to set up your environment and project, and to understand how PHP apps are structured in App Engine. Write down and save your project ID, because you will need it to run the sample application described in this document.

Clone the repository

Download (clone) the sample:

git clone
cd php-docs-samples/appengine/flexible/datastore

Edit project configuration and set dependencies

Install dependencies using Composer:

composer install

Application code

The sample application logs, retrieves, and displays visitor IPs.

The application first instantiates a Datastore mode client:

$datastore = new DatastoreClient([
    'projectId' => $projectId

Each visit is an entity with fields for the visitor's IP address and a timestamp, and is saved to Datastore mode using the insert command:

// Create an entity to insert into datastore.
$key = $datastore->key('visit');
$entity = $datastore->entity($key, [
    'user_ip' => $user_ip,
    'timestamp' => new DateTime(),

The ten most recent visits are retrieved in descending order using the query command:

// Query recent visits.
$query = $datastore->query()
    ->order('timestamp', 'DESCENDING')
$results = $datastore->runQuery($query);
$visits = [];
foreach ($results as $entity) {
    $visits[] = sprintf('Time: %s Addr: %s',
        $entity['timestamp']->format('Y-m-d H:i:s'),

Using index.yaml files

The sample app performs simple queries. More elaborate Datastore mode queries require one or more indexes, which you must specify in an index.yaml file that you upload along with your app. This file may be created manually, or generated automatically while testing your app locally.

Local testing

If you need to develop and test your application locally, you can use the Datastore mode emulator.

For more information

For complete information on Datastore mode, including optimizations and concepts, see the Firestore in Datastore mode documentation.