4

如何选择 PyMongo 只有唯一的记录?

>>> 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

如何在响应查询时获得唯一的“街道”记录?也就是说,为了避免重复记录:

Flatbush Avenue
Flatlands Avenue
Flushing Avenue
4

1 回答 1

10

根据文档 - Cursor.distinct应该可以解决问题:

db.houses.find({"street":{"$regex": "май", "$options":"i"}}).distinct("street")
于 2012-04-05T04:57:57.247 回答