Ruslan Sharipov
Ruslan Sharipov

Reputation: 733

How to make a selection PyMongo only unique records?

How to make a selection PyMongo only unique records?

>>> db.houses.find({"street":{"$regex": "Fl", "$options":"i"}}).count()
107
>>> for item in db.houses.find({"street":{"$regex": "Fl", "$options":"i"}}):
...  print item["street"]
...
Flatbush Avenue
Flatbush Avenue
Flatbush Avenue
Flatlands Avenue
Flatlands Avenue
Flatlands Avenue
Flatlands Avenue
Flatlands Avenue
Flushing Avenue
Flushing Avenue
...more

How to get a unique record only "street" in response to a query? That is, to avoid duplicate records:

Flatbush Avenue
Flatlands Avenue
Flushing Avenue

Upvotes: 4

Views: 1618

Answers (1)

tzaman
tzaman

Reputation: 47770

According to the docs - Cursor.distinct should do the trick:

db.houses.find({"street":{"$regex": "май", "$options":"i"}}).distinct("street")

Upvotes: 10

Related Questions