Home > OS >  Extract data based on hours from mongoDb
Extract data based on hours from mongoDb

Time:12-11

I am trying to extract data from MongoDB.

My raw Db file has a query including date and time(query name 'updateTime').

Data are updated after every 1 minute.

To match this DB data with other Data I only need to extract data from the hourly updates.

for example, I need data from only the following update queries:

"updateTime" : "2021-11-30 20:00:00", 

"updateTime" : "2021-11-30 21:00:00", 

"updateTime" : "2021-11-30 22:00:00", 
.............

I don't want data from each minute, right now my python code extracts data from each minute:

I have code here --- login to DD and loop to sub DB

After the loop, I used the following code to extract data based on query cno.

query = {
    'cno': 10,
 
}
projection = {  '_id':False,
              'updateTime': True,
                'cno': True,
                'pressure':True,
                'radius':True,
                'items.typeA':True}

I search in MongoDB homepage about extracting data and found 'aggregation'(https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromString/) but didn't get any idea to apply in my 'projection' part.

How can I just extract data which are updated after each hour.?

Any help or suggestions?

Thank you.

CodePudding user response:

May be you can add a line in your query like this:

query = {
    'cno': 10,
    'updatetime': {'$regex':'000$'} #if time is ended with 000.
 
}

About $regex : https://docs.mongodb.com/manual/reference/operator/query/regex/

It provides regular expression capabilities for pattern matching strings in queries

  • Related