【问题标题】:$and query in mongodb is not resturning resultsmongodb 中的 $and 查询不返回结果
【发布时间】:2019-01-12 15:37:41
【问题描述】:

我一般是使用 mongodb 和 NoSql 的新手。

这是我尝试查询的 json 代码示例

    {
    "_id" : ObjectId("5c351ae3d85ef0793dacc041"),
    "Year" : 2008,
    "State" : "NY",
    "Producer Type" : "Commercial Cogen",
    "Energy Source" : "All Sources",
    "CO2
  (Metric Tons)" : "590,598",
    "SO2
  (Metric Tons)" : "1,094",
    "NOx
  (Metric Tons)" : "2,390"

}

我正在尝试返回将州显示为 NY 并将能源显示为煤炭的所有结果(如此多的结果)。

db.emissions.find({ $and:[{"state":"NY"}, {"energy source": "coal"}]})

它没有返回任何结果。

谢谢!

编辑:

 db.emissions.find("state":"NY","energy source":"coal")

db.emissions.find({ "State":"NY", "Energy Source": "Coal"})

不幸的是也不能正常工作

但只需在$and 查询中整理区分大小写的字母就可以了!谢谢!

【问题讨论】:

  • 哇,谢谢! @saravana,似乎确实与区分大小写有关!甚至“煤炭”也是大写 C。

标签: python mongodb nosql


【解决方案1】:

你试过了吗

{ field1: <value>, field2: <value> ... }

db.emissions.find( "state":"NY", "energy source": "coal")

小心我认为“能源”“能源”

问候

【讨论】:

    【解决方案2】:

    字段名称区分大小写

    请尝试

    db.emissions.find({ $and:[{"State":"NY"}, {"Energy Source": "coal"}]})
    

    或者干脆

    db.emissions.find({ "State":"NY", "Energy Source": "coal"})
    

    【讨论】:

      猜你喜欢
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多