Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Node script to upgrade GeoFirestore v3 (and older) docs to GeoFirestore v4
const admin = require("firebase-admin");
const { GeoFirestore, GeoCollectionReference } = require("geofirestore");
* Generate new private key by selecting a project from below:
const SERVICE_ACCOUNT = require("./serviceAccountKey.json");
* Set the collection to update here.
const COLLECTION_NAME = "restaurants";
* If you use a custom key, define it here, otherwise leave this.
const CUSTOM_KEY = "coordinates";
credential: admin.credential.cert(SERVICE_ACCOUNT),
databaseURL: "https://" + SERVICE_ACCOUNT["project_id"] + "",
const firestore = admin.firestore();
const geofirestore = new GeoFirestore(firestore);
const collection = firestore.collection(COLLECTION_NAME);
const geocollection = new GeoCollectionReference(collection);
let totalUpdated = 0;
function updateCollection(query) {
return new Promise((resolve, reject) =>
deleteQueryBatch(query.limit(500), resolve, reject)
function deleteQueryBatch(query, resolve, reject) {
.then((snapshot) => {
// When there are no documents left, we are done
if (snapshot.size === 0) {
console.log(`DONE, UPDATED ${totalUpdated}`);
return 0;
`UPDATING ${snapshot.size} GEODOCUMENTS, ALREADY UPDATED ${totalUpdated}`
// Update documents in a batch
const batch = geofirestore.batch(); => {
const geodoc = geocollection.doc(;
batch.set(geodoc,, { customKey: CUSTOM_KEY });
return batch.commit().then(() => snapshot.size);
.catch((e) => {
"This script was unable to convert your collection for GeoFirestore v4.",
return 0;
.then((numUpdated) => {
totalUpdated += numUpdated;
if (numUpdated === 0) {
process.nextTick(() => deleteQueryBatch(query, resolve, reject));
.catch((err) => reject(err));
(async () => {
await updateCollection(collection.orderBy("g").orderBy("l").orderBy("d"));
Copy link

smolugu commented Sep 21, 2020

Hi, I am using 3.4.1. currently. Are there any major improvements in 4.0.

Also I am using this in google cloud functions in firebase project. Can you tell me how to run this script?


Copy link

MichaelSolati commented Sep 21, 2020

There are no major improvements in performance, just restructured data. As far as running the script I'd advise you go through the code above and make sure it makes sense for you. I don't want to be responsible for accidentally corrupting your data, so please read it and make sure you want to execute it. This is not code you'd run on a cloud function, it's something you'd run locally.

Copy link

smolugu commented Sep 23, 2020

@MichaelSolati Thank you!

Also, I am getting empty results when I use where condition as below
const value = await query.where('notifications', '==', true).get();

I thought "notifications" field needs to be in all firestore documents of a collection. so updated all documents with "notifications" field but still the query returns empty set of documents.

Can you please let me know if it is possible to filter the query results using where clause?


Copy link

cyrilblanchet commented Oct 25, 2020

I think you can delete the line #20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment