How to fetch date iterating through each document?

I want to plot similar to time-series values. Here in one of the documents, I have multiple timestamps and each timestamp has a minutes object which contains an Array with Second, MilliSecond, Value data. I want to:

  1. Fetch the time and plot it say on X-axis with respect to the time (hours+minutes+seconds+milliseconds) for this I need to understand how i can reiiterate through the document to get this done ? Using a Python Code.
    FetchingDate

It would be better if we had your documents in JSON rather than an image.

Re-typing your sample document is really time consuming.

Read Formatting code and log snippets in posts before re-posting to make sure the documents are usable.

And

is kind of outside the scope of this forum. Once you get your document in a python variable, it is pure python code.

// Get the object field minutes from the document
minutes = document.get( "minutes" )

// Get a specific minute data within the minutes Object
data = minutes.get( "0" ).get( "data" )

Why is minutes and Object rather than an array?

Once you have your data list you simply loop using one of ForLoop - Python Wiki.

minutes = machine_parts_processed_hour.get( “minutes” ) 12 #Get a specific minute data within the minutes Object 13 data = minutes.get( “0” ).get( “data” ) File TypeError : ‘Collection’ object is not callable. If you meant to call the ‘get’ method on a ‘Collection’ object it is failing because no such method exists


_id 60c7753df65559ee0e8e64e5 ObjectId
sensor 60c7751c84ed310f14db54c7 ObjectId
time 2021-06-14T00:00:00.000+00:00 Date
minutes Object Object 
0 Object Object
data Array Array
0 Object Object
s 0 Int32
ms 250000 Int32
value 0 Int32
nok false Int32

Have you try to cut-n-paste the document

into mongosh or compass? Did it work? It did not for me? See Formatting code and log snippets in posts

This is not what I meant. What I wrote is:

You use your normal code to get (find) the document and assign it to a variable. The get method is a normal python dictionary method to

Yes. And if it didn’t worked for you, please paste the following:

 { "_id" : ObjectId("60d340b7f65559ee0e92eae6"), "sensor" : ObjectId("60d3212261328d07e9022a7f"), "time" : ISODate("2021-06-23T14:00:00Z"), "minutes" : { "9" : { "data" : [ { "s" : 58, "ms" : 405837, "value" : 36, "nok" : false }, { "s" : 59, "ms" : 308719, "value" : 37, "nok" : false, "cycle_time" : 0.902882 } ], "data_count" : 2, "data_sum" : 73, "data_min" : 0, "data_max" : 37, "ok_count" : 2, "ok_sum" : 73, "ok_min" : 36, "ok_max" : 37, "cycle_time_count" : 1, "cycle_time_avg" : 0.902882 }, "10" : { "data" : [ { "s" : 31, "ms" : 49953, "value" : 38, "nok" : false, "cycle_time" : 31.741234 }, { "s" : 31, "ms" : 49953, "value" : 38, "nok" : false } ], "data_count" : 2, "data_sum" : 76, "data_min" : 0, "data_max" : 38, "ok_count" : 2, "ok_sum" : 76, "ok_min" : 38, "ok_max" : 38, "cycle_time_count" : 1, "cycle_time_avg" : 31.741234 }, "28" : { "data" : [ { "s" : 26, "ms" : 56384, "value" : 39, "nok" : false }, { "s" : 26, "ms" : 542060, "value" : 40, "nok" : false, "cycle_time" : 0.485676 }, { "s" : 38, "ms" : 617859, "value" : 41, "nok" : false, "cycle_time" : 12.075799 }, { "s" : 38, "ms" : 617859, "value" : 41, "nok" : false } ], "data_count" : 4, "data_sum" : 161, "data_min" : 0, "data_max" : 41, "ok_count" : 4, "ok_sum" : 161, "ok_min" : 39, "ok_max" : 41, "cycle_time_count" : 2, "cycle_time_avg" : 6.2807375 }, "45" : { "data" : [ { "s" : 38, "ms" : 308836, "value" : 42, "nok" : false } ], "data_count" : 1, "data_sum" : 42, "data_min" : 0, "data_max" : 42, "ok_count" : 1, "ok_sum" : 42, "ok_min" : 42, "ok_max" : 42 }, "48" : { "data" : [ { "s" : 22, "ms" : 667861, "value" : 43, "nok" : false } ], "data_count" : 1, "data_sum" : 43, "data_min" : 0, "data_max" : 43, "ok_count" : 1, "ok_sum" : 43, "ok_min" : 43, "ok_max" : 43 }, "49" : { "data" : [ { "s" : 10, "ms" : 874242, "value" : 44, "nok" : false, "cycle_time" : 48.206381 }, { "s" : 10, "ms" : 874242, "value" : 44, "nok" : false } ], "data_count" : 2, "data_sum" : 88, "data_min" : 0, "data_max" : 44, "ok_count" : 2, "ok_sum" : 88, "ok_min" : 44, "ok_max" : 44, "cycle_time_count" : 1, "cycle_time_avg" : 48.206381 }, "52" : { "data" : [ { "s" : 40, "ms" : 665866, "value" : 45, "nok" : false } ], "data_count" : 1, "data_sum" : 45, "data_min" : 0, "data_max" : 45, "ok_count" : 1, "ok_sum" : 45, "ok_min" : 45, "ok_max" : 45 }, "53" : { "data" : [ { "s" : 20, "ms" : 693017, "value" : 46, "nok" : false, "cycle_time" : 40.027151 }, { "s" : 20, "ms" : 693017, "value" : 46, "nok" : false } ], "data_count" : 2, "data_sum" : 92, "data_min" : 0, "data_max" : 46, "ok_count" : 2, "ok_sum" : 92, "ok_min" : 46, "ok_max" : 46, "cycle_time_count" : 1, "cycle_time_avg" : 40.027151 }, "54" : { "data" : [ { "s" : 35, "ms" : 622081, "value" : 47, "nok" : false, "cycle_time" : 74.929064 }, { "s" : 37, "ms" : 67781, "value" : 48, "nok" : false, "cycle_time" : 1.4457 }, { "s" : 37, "ms" : 67781, "value" : 48, "nok" : false } ], "data_count" : 3, "data_sum" : 143, "data_min" : 0, "data_max" : 48, "ok_count" : 3, "ok_sum" : 143, "ok_min" : 47, "ok_max" : 48, "cycle_time_count" : 2, "cycle_time_avg" : 38.187382 } }, "data_count" : 18, "data_sum" : 763, "data_min" : 0, "data_max" : 48, "ok_count" : 18, "ok_sum" : 763, "ok_min" : 36, "ok_max" : 48, "cycle_time_count" : 8, "cycle_time_avg" : 26.226735874999996 }```

> Blockquote

Understood, however, what I want is to traverse through each of the "minutes" data to plot a time series with respect to the seconds and milliseconds mentioned on X-axis and Values mentioned on Y-Axis.

Finally got your sample document.

Using python3

>>> document = { "_id" : ObjectId("60d340b7f65559ee0e92eae6"), "sensor" : Object ... }
>>> var_minutes = document.get( "minutes" )
>>> # by just typing var_minutes you will see that you got the object that has the
... # key "minutes" and that starts with { "9" : { "data" : ...
9" 
>>> var_minutes

This is just pure python. To get the 9th minute you simply do

>>> var_9th_minute = var_minutes.get( "9" )

The above will be your first hurdle because minutes is not an array but an object. You will need to use a python for-loop (link to documentation already provide) to iterate over all minutes but you would need to convert your number index of your for-loop to a string in order to get the given minute data.

This will give you the object with the key “9” that starts with

{ "data" : [ { "s" : ... 

This is standard python.