title: In pymongo, how to get a set of distinct values from a collection?
body: <p>My environment consists of a mongodb docker container and a simple flask application.</p>
  
  <pre><code>MongoDB: 3.7.1
  Pymongo: pymongo==3.6.0
  </code></pre>
  
  <p>I tried several different ways of constructing the distinct syntax.
  I'm getting empty results from pymongo:</p>
  
  <pre><code>from flask import Flask, render_template, redirect
  from pymongo import MongoClient
  
  app = Flask(__name__)
  app.config.update(dict(SECRET_KEY='asdfghjkl'  
  client = MongoClient('mongodb://foouser:foopwd@localhost:27017')
  
  @app.route('/get_uniq_ips', methods=['GET'])
  def get_uniq_ips():
      db = client.sshnawks
      uniqips = db.actors.distinct('src_ip')
  #
  # works, returns all values from collection showing src_ip field
  #    uniqips = db.actors.find({}, {'src_ip': 1})
  # empty sets
  #    uniqips = db.actors.distinct({}, 'src_ip')
  #    uniqips = db.actors.find({}, {'src_ip': 1}).distinct('src_ip')
      ipl = []
      for i in uniqips:
          ipl.append(i)
      return render_template('uniqips.html', ipl=ipl)
  
  @app.route('/', methods=['GET'])
  def main():
  return render_template('index.html')
  
  if __name__=='__main__':
  app.run(debug=True)
  </code></pre>
  
  <p>Here is pertinent section of the the template:</p>
  
  <pre><code>&lt;tbody&gt;
  {% for i in ipl %}
    &lt;tr&gt;
      &lt;td&gt;{{ i["src_ip"] }}&lt;/td&gt;
    &lt;/tr&gt;
  {% endfor %}
  &lt;/tbody&gt;
  </code></pre>
  
  <p>What happens when the template is rendered is that the  tags are inserted into the document but without a value. I have counted the  tags returned, its the same number as the total number of documents in the collection.</p>
  
  <p>Am I doing something wrong with distinct?</p>
  
  <p>I tried several other hacks for processing the data returned from find(). However, I wasn't able to see a difference between the original list and the curated list.</p>
  
  <p>for example, i tried this trick for reducing the list to a set:</p>
  
  <pre><code>ipl = []
  for i in uniqips:
       if i not in ipl:
           ipl.append(i)
   return render_template('uniqips.html', ipl=ipl)
  </code></pre>
  
  <p>ipl list had the same list of values as that contained in uniqips. Meaning, if i removed the 'if' line inside the for loop, i get the same list as I do when its there.</p>
  
  <p>Heres what the documents looks like:</p>
  
  <pre><code>{ "_id" : ObjectId("5a89c268d15f5853babff842"), "src_ip" : "158.248.181.217", "interface" : "re0:", "direction" : "in", "rule_num" : "rule 4/(match)", "src_port" : "36790", "dst_port" : "22", "action" : "block", "dst_ip" : "1.2.3.4", "unix_epoch" : "1517220734.472831" }
  { "_id" : ObjectId("5a89c268d15f5853babff843"), "src_ip" : "223.240.222.100", "interface" : "re0:", "direction" : "in", "rule_num" : "rule 4/(match)", "src_port" : "7496", "dst_port" : "22", "action" : "block", "dst_ip" : "1.2.3.4", "unix_epoch" : "1517222539.564746" }
  { "_id" : ObjectId("5a89c268d15f5853babff844"), "src_ip" : "195.154.47.194", "interface" : "re0:", "direction" : "in", "rule_num" : "rule 4/(match)", "src_port" : "41609", "dst_port" : "22", "action" : "block", "dst_ip" : "1.2.3.4", "unix_epoch" : "1517222903.470985" }
  { "_id" : ObjectId("5a89c268d15f5853babff845"), "src_ip" : "103.79.141.180", "interface" : "re0:", "direction" : "in", "rule_num" : "rule 4/(match)", "src_port" : "53789", "dst_port" : "22", "action" : "block", "dst_ip" : "1.2.3.4", "unix_epoch" : "1517225309.861815" }
  </code></pre>
  
  <p>What am I doing wrong?</p>