gpt4 book ai didi

python - 如何从 pymongo 字段中的所有值创建列表?

转载 作者:行者123 更新时间:2023-12-01 08:31:24 26 4
gpt4 key购买 nike

我有一个具有以下结构的单词数据库:

{
initial: "t"
count: 3,
words: [
{value: "the", tweets: [{"tweet_id": <some-tweet-id>, "pos": (2, 5)},
{"tweet_id": <some-other-tweet-id>, "pos": (9, 12)}]},
{value: "turkish", tweets: [{"tweet_id": <some-tweet-id>, "pos": (5, 11)}]}
]

}

我正在此数据库中存储数百万条推文中存在的所有独特单词及其位置。问题是我想列出所有以首字母开头的单词。为了更清楚地说明,我想做类似的事情:

cur = db.tweet_words.find({"initial": initial})
words = list(next(cur)["words"])

如何在不扭曲数据库当前架构的情况下实现这一目标? (要了解为什么我需要保留架构,请参阅 this question )

最佳答案

您可以使用distinct为此:

db.tweet_words.distinct("words.value", {"initial": initial})

输出:

[ "the", "turkish" ]

关于python - 如何从 pymongo 字段中的所有值创建列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53900040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com