【问题标题】:How can I find distinct items in a collection excluding a particular value in MongoDB?如何在 MongoDB 中找到不包括特定值的集合中的不同项目?
【发布时间】:2016-04-28 00:15:48
【问题描述】:

我想在集合中选择字段“name”的所有不同值,条件是字段“sex”必须是“male”并且字段“state”不能是“FR” (所有不同的发明者的名字都是男性而不是法国人)。

我已经尝试过这段代码,但找不到任何结果。对吗?

inventors = db.collection.distinct("name", {"$and": [{"sex":"male"}, {"state": {"$nin": "FR"}}]})

我使用 PyCharm 作为 IDE

【问题讨论】:

    标签: python mongodb collections pymongo distinct-values


    【解决方案1】:

    使用"not equal" 代替“不在”。替换:

    {"$nin": "FR"}
    

    与:

    {"$ne": "FR"}
    

    【讨论】:

    • 我刚刚尝试了这两个运算符。没有结果
    • @DiegoFosso 您确定您连接到正确的 mongodb 实例并使用正确的集合吗? db.collection.distinct 将查询 collection 集合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2017-07-07
    • 1970-01-01
    • 2023-03-07
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多