user5674731
user5674731

Reputation:

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

I have a mongoDB database that store several JSON files like this one :

{   "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_heard = [2016-01-18T18:41:29.559Z, 2016-01-18T18:32:28.271Z, ...]

result = [21.511440541411535, 21.108604576216564, ...]

Upvotes: 1

Views: 2838

Answers (1)

chridam
chridam

Reputation: 103455

Use the distinct() method:

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

Upvotes: 1

Related Questions