nodes: 0, module; 1, function_definition; 2, function_name:get_collection_documents_generator; 3, parameters; 4, block; 5, identifier:client; 6, identifier:database_name; 7, identifier:collection_name; 8, identifier:spec; 9, identifier:latest_n; 10, identifier:sort_key; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, if_statement; 16, for_statement; 17, comment:"""
      This is a python generator that yields tweets stored in a mongodb collection.
  
      Tweet "created_at" field is assumed to have been stored in the format supported by MongoDB.
  
      Inputs: - client: A pymongo MongoClient object.
              - database_name: The name of a Mongo database as a string.
              - collection_name: The name of the tweet collection as a string.
              - spec: A python dictionary that defines higher query arguments.
              - latest_n: The number of latest results we require from the mongo document collection.
              - sort_key: A field name according to which we will sort in ascending order.
  
      Yields: - document: A document in python dictionary (json) format.
      """; 18, assignment; 19, assignment; 20, call; 21, comparison_operator:latest_n is not None; 22, block; 23, else_clause; 24, identifier:document; 25, identifier:cursor; 26, block; 27, identifier:mongo_database; 28, subscript; 29, identifier:collection; 30, subscript; 31, attribute; 32, argument_list; 33, identifier:latest_n; 34, None; 35, expression_statement; 36, if_statement; 37, expression_statement; 38, expression_statement; 39, block; 40, expression_statement; 41, identifier:client; 42, identifier:database_name; 43, identifier:mongo_database; 44, identifier:collection_name; 45, identifier:collection; 46, identifier:create_index; 47, identifier:sort_key; 48, assignment; 49, comparison_operator:collection.count() - latest_n < 0; 50, block; 51, assignment; 52, assignment; 53, expression_statement; 54, yield; 55, identifier:skip_n; 56, binary_operator:collection.count() - latest_n; 57, binary_operator:collection.count() - latest_n; 58, integer:0; 59, expression_statement; 60, identifier:cursor; 61, call; 62, identifier:cursor; 63, subscript; 64, assignment; 65, identifier:document; 66, call; 67, identifier:latest_n; 68, call; 69, identifier:latest_n; 70, assignment; 71, attribute; 72, argument_list; 73, identifier:cursor; 74, slice; 75, identifier:cursor; 76, call; 77, attribute; 78, argument_list; 79, attribute; 80, argument_list; 81, identifier:skip_n; 82, integer:0; 83, call; 84, identifier:sort; 85, list; 86, identifier:skip_n; 87, attribute; 88, argument_list; 89, identifier:collection; 90, identifier:count; 91, identifier:collection; 92, identifier:count; 93, attribute; 94, argument_list; 95, tuple; 96, call; 97, identifier:sort; 98, list; 99, identifier:collection; 100, identifier:find; 101, keyword_argument; 102, identifier:sort_key; 103, identifier:ASCENDING; 104, attribute; 105, argument_list; 106, tuple; 107, identifier:filter; 108, identifier:spec; 109, identifier:collection; 110, identifier:find; 111, keyword_argument; 112, identifier:sort_key; 113, identifier:ASCENDING; 114, identifier:filter; 115, identifier:spec
code: def get_collection_documents_generator(client, database_name, collection_name, spec, latest_n, sort_key):
      """
      This is a python generator that yields tweets stored in a mongodb collection.
  
      Tweet "created_at" field is assumed to have been stored in the format supported by MongoDB.
  
      Inputs: - client: A pymongo MongoClient object.
              - database_name: The name of a Mongo database as a string.
              - collection_name: The name of the tweet collection as a string.
              - spec: A python dictionary that defines higher query arguments.
              - latest_n: The number of latest results we require from the mongo document collection.
              - sort_key: A field name according to which we will sort in ascending order.
  
      Yields: - document: A document in python dictionary (json) format.
      """
      mongo_database = client[database_name]
      collection = mongo_database[collection_name]
      collection.create_index(sort_key)
  
      if latest_n is not None:
          skip_n = collection.count() - latest_n
          if collection.count() - latest_n < 0:
              skip_n = 0
          cursor = collection.find(filter=spec).sort([(sort_key, ASCENDING), ])
          cursor = cursor[skip_n:]
      else:
          cursor = collection.find(filter=spec).sort([(sort_key, ASCENDING), ])
  
      for document in cursor:
          yield document