Townheadbluesboy
Townheadbluesboy

Reputation: 385

How to get distinct values from PyMongo

In MongoDB I have a dataset of store data. Using PyMongo I am finding all of the distinct/unique values in a collection

for testy in collection.distinct('stores'):
print(testy)

I can also find a subset of badly spelt stores I'm interested in

for testy in collection.find({'stores': {'$in': ['Aldi','ALDI','aldi']}}):

What I want to do is find the unique in this subset

According to the MongoDB docs

db.runCommand ( { distinct: "inventory", key: "item.sku", query: { dept: "A"} } )

Tried lots of combinations adding the query doing the $in but can't get it to work.

Upvotes: 9

Views: 16043

Answers (1)

Sede
Sede

Reputation: 61273

What you are looking for is distinct

for testy in collection.find().distinct('stores'):
    print(testy)

or

for testy in collection.distinct('stores', {'dept': 'A'}):
    print(testy)

Upvotes: 16

Related Questions