Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 816
Ruslan Sharipov Avatar asked Dec 28 '22 03:12

Ruslan Sharipov


1 Answers

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

db.houses.find({"street":{"$regex": "май", "$options":"i"}}).distinct("street")
like image 111
tzaman Avatar answered Jan 11 '23 20:01

tzaman