Add docs to a collection in Python

:arrow_down: Below is a mini example code of how to add docs to a collection. Code assumes the collection is created.

You can refer to this to create and poll a collection until it’s ready:

Requirements before running the script

  • Create an API KEY on the Rockset Console
  • Create a .env file and add your APIKEY.
  • pip3 install the Rockset module
  • pip3 install the dotenv
from rockset import Client
from dotenv import load_dotenv
import rockset, os
load_dotenv()


ROCKSETAPIKEY = os.environ.get('APIKEY')
collection_name = "SampleCollection"
client = Client(api_server='api.rs2.usw2.rockset.com',
                api_key=ROCKSETAPIKEY)


def add_document():
  """ add docs"""
  # you can write if/else statement or try/except to see if collection exists, add docs,
  # or create collection if not, then add docs.

  # collection_exists = client.Collection.retrieve(collection_name)

  print("adding doc")
  doc = [{
    "id":"a1", "price":"$100", "latitude": "37.7809", "longitude": "-122.4341"
  }]

  client.Collection.add_docs(collection_name, doc)

def main():
  add_document()

if __name__=='__main__':
  main()