如何在pymongo中找到特定键的所有值?

时间:2016-01-18 18:50:42

标签: python json mongodb

我有一个mongoDB数据库,可以存储几个这样的JSON文件:

{   "cmd": "VarReturn",
    "name": "temp",
    "result": 21.511440541411535,
    "coreInfo": {
      "last_app": "",
      "last_heard": "2016-01-18T18:41:29.559Z",
      "connected": true,
      "last_handshake_at": "2016-01-18T18:06:02.795Z",
      "deviceID": "X",
      "product_id": 6 
    } 
}

我想查询last_heardresult的所有值,以便得到类似的内容:

last_heard = [2016-01-18T18:41:29.559Z, 2016-01-18T18:32:28.271Z, ...]

result = [21.511440541411535, 21.108604576216564, ...]

1 个答案:

答案 0 :(得分:1)

使用 distinct() 方法:

last_heard = db.collection.distinct("coreInfo.last_heard")
result = db.collection.distinct("result")