简体   繁体   English

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

[英]How to find all values for a specific key in pymongo?

I have a mongoDB database that store several JSON files like this one : 我有一个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 
    } 
}

I want to query all values for last_heard and result , in order to get something like this : 我想查询last_heardresult所有值,以获得类似这样的内容:

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

result = [21.511440541411535, 21.108604576216564, ...]

Use the distinct() method: 使用distinct()方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM