Santiago M. Quintero
Santiago M. Quintero

Reputation: 1273

How to write location, reference and timestamp data types on Firestore with Python's SDK?

Documentation goes as far as explaining how to write the following data types:

data = {
  u'stringExample': u'Hello, World!',
  u'booleanExample': True,
  u'numberExample': 3.14159265,
  u'dateExample': datetime.datetime.now(),
  u'arrayExample': [5, True, u'hello'],
  u'nullExample': None,
  u'objectExample': {
    u'a': 5,
    u'b': True
  }
}

I am migrating a Mongo DB collection into FireStore.

This is my code so far:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

def initialize_firestore():
  cred = credentials.Certificate('admin.json')
  app = firebase_admin.initialize_app(cred)
  db = firestore.client()
  return db

db.collection('My_Collection').document('desired_iD').set('Document_to_write')

I would like to write a document with reference and location fields plus a custom date.

Upvotes: 1

Views: 764

Answers (1)

Santiago M. Quintero
Santiago M. Quintero

Reputation: 1273

After getting the response(res) & initializing dictionary (Document_to_write):

Document_to_write['date_type'] = datetime.datetime.strptime(res['created'], "date format in string")
Document_to_write['reference_type'] = db.document(u'Collection_Name/'+res['collection_id'])
Document_to_write['location_type'] = firestore.GeoPoint(res['latitude'], res['longitude'])

Follow to save with selected method.

Upvotes: 1

Related Questions