如何从PyMongo获取不同的值

时间:2021-10-19 13:36:09

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

在MongoDB中,我有一个商店数据的数据集。使用PyMongo我在集合中找到所有不同/唯一的值

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

根据MongoDB文档

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.

尝试了许多组合添加查询执行$ in但无法使其工作。

1 个解决方案

#1


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)

#1


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)