【问题标题】:Mongodb find all except from one or two criteriaMongodb 查找除一两个条件之外的所有内容
【发布时间】:2013-08-28 16:13:49
【问题描述】:

好吧,我运行一个字段匹配:

db.bios.find( { "Country":"Netherlands" } )

我怎样才能带上所有文件,但不带"Country":"Netherlands" 的文件?

是否也可以携带所有文件但没有两个国家/地区?

【问题讨论】:

    标签: mongodb pymongo mongodb-query


    【解决方案1】:

    使用$nin operator

    例如:

    db.bios.find( { Country: { $nin: ["Country1", "Country2"] } } )
    

    $ne 仅代表一个国家/地区:

    db.bios.find( { Country: { $ne: "Country1" } } )
    

    【讨论】:

      【解决方案2】:

      您可以将$ne-operator(不等于)用于单个值。

      db.bios.find( { "Country": { $ne: "Netherlands" } } );
      

      要排除多个值,您可以使用$nin(not-in)运算符,它允许您传递一组值:

      db.bios.find( { "Country": { $nin: [ "Netherlands", "Belgium", "Luxembourg" ] } );
      

      【讨论】:

        猜你喜欢
        • 2020-09-06
        • 1970-01-01
        • 2022-07-29
        • 1970-01-01
        • 2014-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-26
        相关资源
        最近更新 更多