nodes: 0, module; 1, function_definition; 2, function_name:query_mongo_sort_decend; 3, parameters; 4, block; 5, identifier:database_name; 6, identifier:collection_name; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, try_statement; 16, return_statement; 17, identifier:query; 18, dictionary; 19, identifier:skip; 20, integer:0; 21, identifier:limit; 22, call; 23, identifier:return_keys; 24, tuple; 25, identifier:sortkey; 26, None; 27, comment:"""return a response_dict  with a list of search results in decending
      order based on a sort key
      """; 28, assignment; 29, assignment; 30, block; 31, except_clause; 32, identifier:response_dict; 33, identifier:getattr; 34, argument_list; 35, identifier:l; 36, list; 37, identifier:response_dict; 38, dictionary; 39, expression_statement; 40, expression_statement; 41, expression_statement; 42, expression_statement; 43, if_statement; 44, comment:# response_dict['num_results']=int(mysearchresult.count(with_limit_and_skip=False)); 45, expression_statement; 46, expression_statement; 47, for_statement; 48, expression_statement; 49, block; 50, identifier:settings; 51, string; 52, integer:200; 53, assignment; 54, assignment; 55, assignment; 56, assignment; 57, identifier:return_keys; 58, block; 59, else_clause; 60, assignment; 61, assignment; 62, identifier:d; 63, identifier:mysearchresult; 64, block; 65, assignment; 66, expression_statement; 67, expression_statement; 68, expression_statement; 69, expression_statement; 70, expression_statement; 71, expression_statement; 72, expression_statement; 73, string_content:MONGO_LIMIT; 74, identifier:mongodb_client_url; 75, call; 76, identifier:mc; 77, call; 78, identifier:db; 79, subscript; 80, identifier:collection; 81, subscript; 82, expression_statement; 83, for_statement; 84, comment:# print "returndict=",return_dict; 85, expression_statement; 86, block; 87, subscript; 88, integer:200; 89, subscript; 90, string:"search-results"; 91, expression_statement; 92, delete_statement; 93, expression_statement; 94, subscript; 95, identifier:l; 96, call; 97, call; 98, assignment; 99, assignment; 100, assignment; 101, assignment; 102, assignment; 103, identifier:getattr; 104, argument_list; 105, identifier:MongoClient; 106, argument_list; 107, identifier:mc; 108, call; 109, identifier:db; 110, call; 111, assignment; 112, identifier:k; 113, identifier:return_keys; 114, block; 115, assignment; 116, expression_statement; 117, identifier:response_dict; 118, string; 119, identifier:response_dict; 120, string; 121, assignment; 122, subscript; 123, call; 124, identifier:response_dict; 125, string; 126, identifier:print; 127, argument_list; 128, identifier:print; 129, argument_list; 130, subscript; 131, integer:0; 132, subscript; 133, integer:500; 134, subscript; 135, string:"Error"; 136, subscript; 137, list; 138, subscript; 139, call; 140, identifier:settings; 141, string; 142, string; 143, identifier:mongodb_client_url; 144, keyword_argument; 145, identifier:str; 146, argument_list; 147, identifier:str; 148, argument_list; 149, identifier:return_dict; 150, dictionary; 151, expression_statement; 152, identifier:mysearchresult; 153, call; 154, assignment; 155, string_content:code; 156, string_content:type; 157, subscript; 158, call; 159, identifier:d; 160, string; 161, attribute; 162, argument_list; 163, string_content:results; 164, string:"Error reading from Mongo"; 165, call; 166, identifier:response_dict; 167, string; 168, identifier:response_dict; 169, string; 170, identifier:response_dict; 171, string; 172, identifier:response_dict; 173, string; 174, identifier:response_dict; 175, string; 176, identifier:str; 177, argument_list; 178, string_content:MONGODB_CLIENT; 179, string_content:mongodb://localhost:27017/; 180, identifier:document_class; 181, identifier:OrderedDict; 182, identifier:database_name; 183, identifier:collection_name; 184, assignment; 185, attribute; 186, argument_list; 187, identifier:mysearchresult; 188, call; 189, identifier:d; 190, string; 191, attribute; 192, argument_list; 193, string_content:_id; 194, identifier:l; 195, identifier:append; 196, identifier:d; 197, identifier:str; 198, argument_list; 199, string_content:num_results; 200, string_content:code; 201, string_content:type; 202, string_content:results; 203, string_content:message; 204, call; 205, subscript; 206, integer:1; 207, call; 208, identifier:sort; 209, identifier:sortkey; 210, identifier:DESCENDING; 211, attribute; 212, argument_list; 213, string_content:id; 214, subscript; 215, identifier:__str__; 216, call; 217, attribute; 218, argument_list; 219, identifier:return_dict; 220, identifier:k; 221, attribute; 222, argument_list; 223, call; 224, identifier:sort; 225, identifier:sortkey; 226, identifier:DESCENDING; 227, identifier:d; 228, string; 229, attribute; 230, argument_list; 231, identifier:sys; 232, identifier:exc_info; 233, call; 234, identifier:limit; 235, identifier:limit; 236, attribute; 237, argument_list; 238, string_content:_id; 239, identifier:sys; 240, identifier:exc_info; 241, attribute; 242, argument_list; 243, call; 244, identifier:limit; 245, identifier:limit; 246, call; 247, identifier:skip; 248, identifier:skip; 249, attribute; 250, argument_list; 251, attribute; 252, argument_list; 253, call; 254, identifier:skip; 255, identifier:skip; 256, identifier:collection; 257, identifier:find; 258, identifier:query; 259, identifier:return_dict; 260, attribute; 261, argument_list; 262, identifier:collection; 263, identifier:find; 264, identifier:query
code: def query_mongo_sort_decend(
          database_name,
          collection_name,
          query={},
          skip=0,
          limit=getattr(
              settings,
              'MONGO_LIMIT',
              200),
      return_keys=(),
          sortkey=None):
      """return a response_dict  with a list of search results in decending
      order based on a sort key
      """
  
      l = []
      response_dict = {}
  
      try:
          mongodb_client_url = getattr(settings, 'MONGODB_CLIENT',
                                       'mongodb://localhost:27017/')
          mc = MongoClient(mongodb_client_url,document_class=OrderedDict)
  
          db = mc[str(database_name)]
          collection = db[str(collection_name)]
  
          if return_keys:
              return_dict = {}
              for k in return_keys:
                  return_dict[k] = 1
              # print "returndict=",return_dict
              mysearchresult = collection.find(
                  query, return_dict).skip(skip).limit(limit).sort(
                  sortkey, DESCENDING)
          else:
              mysearchresult = collection.find(query).skip(
                  skip).limit(limit).sort(sortkey, DESCENDING)
  
          # response_dict['num_results']=int(mysearchresult.count(with_limit_and_skip=False))
          response_dict['code'] = 200
          response_dict['type'] = "search-results"
          for d in mysearchresult:
              d['id'] = d['_id'].__str__()
              del d['_id']
              l.append(d)
          response_dict['results'] = l
  
      except:
          print("Error reading from Mongo")
          print(str(sys.exc_info()))
          response_dict['num_results'] = 0
          response_dict['code'] = 500
          response_dict['type'] = "Error"
          response_dict['results'] = []
          response_dict['message'] = str(sys.exc_info())
      return response_dict