pymongo-如何与其他查询参数一起为字段提供不同的值 [英] pymongo- How can I have distinct values for a field along with other query parameters

查看:254
本文介绍了pymongo-如何与其他查询参数一起为字段提供不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pymongo,并希望为字段提供不同的值,以便我还可以传递其他查询参数.例如,我有如下条目:

I am using pymongo and want to have distinct values for a field such that I can also pass other query parameters. For example, I have entries like:

{
   id = "my_id1"
   tags: [tag1, tag2, tag3],
   category: "movie",
}
{
   id = "my_id2"
   tags: [tag3, tag6, tag9],
   category: "tv",
}
{
   id = "my_id3"
   tags: [tag2, tag6, tag8],
   category: "movie",
}

所以我想在电影类别下放置所有不同的标签.谁能指导我如何使用pymongo实现这一目标.在mongo javascript shell中,我发布了db.mycoll.distinct('tags',{category:"movie"}),它运行良好.但是当我在pymongo中执行相同操作时,会引发错误.我猜pymongo不支持它.尽管知道如何完成这样的任务.

So I want to have all distinct tags under movie category. Can anyone please guide how can I achive this using pymongo. In mongo javascript shell, I issued db.mycoll.distinct('tags', {category: "movie"}) and it worked just fine. But when I do the same in pymongo it raises error. I guess it is not supported in pymongo. Any idea though how can such a task be achieved.

推荐答案

您必须进行distinct调用

You have to make the distinct call on the cursor returned from a find instead of on the collection:

tags = db.mycoll.find({"category": "movie"}).distinct("tags")

这篇关于pymongo-如何与其他查询参数一起为字段提供不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆