Search code examples
pythonpython-3.xmongodbdataframeinitialization

How can I get data from MongoDb with starting and ending date?


I am trying to pull data from MongoDb in pandas data frame but stuck here.

This is my code:

import sys
sys.path.insert(0, "./main/common")
from datetime import datetime, timedelta
import logging
from db_handler import DBhandler


starttime = datetime.now() - timedelta(days=7) 
endtime = datetime.now() - timedelta(days=1)

from_date = starttime.strftime('%Y-%m-%d')
yesterday = endtime.strftime('%Y-%m-%d')

class BmsLog:
    @classmethod
    def list(cls, my_num: int, yyMm: str, dd: str):
        dBHandler = DBhandler()
        dbName = 'bms'
        collName = 'bmsLog_'

        dateAt = yyMm + dd

        bmsLogs = dBHandler.find(
            condition={'my_)number': my_num, 'dateAt': dateAt},
            dbName=f'{dbName}',
            collName=f'{collName}{yyMm}',
        )
        print(bmsLogs)
        logging.info(f'List bms Logs. my_number: {my_number}, yyMm: {yyMm}, dd: {dd}, length: {len(bmsLogs)}')
        
        return bmsLogs

 

I connect my code with DBhandler. This code gives me all data from branch of bms db. But only for a given date. I want to get data from starting to ending data (desirable date). How can i add such function?

Can I get some help?

I would like to include staring date = ....., and ending date = ..... with a my_number. There are many my_number queries in DB but I want to get for specific number with specific data in Dataframe.


Solution

  • I would suggest to use pymongo instead which is the recommended library to work with MongoDB. Then you can use the standard MongoDB calls to get the required data based on the date range with:

    collection = MongoClient(<your credentials>).get_database('bms').get_collection('bmsLog_')
    collection.find({'my_number': my_num, 'dateAt': {'$gte': startDate,'$lte': endDate}})