Search code examples
pythonmongodbpymongo

Pymongo calculation within database


My goal:

calculate:

"open" * 1.1

before pymongo output the data, because I think it would be faster

Data looks like following:

{
    "_id": "000016.XSHE_2020-01-02-09-32",
    "order_book_id": "000016.XSHE",
    "datetime": {
        "$date": "2020-01-02T09:32:00.000Z"
    },
    "num_trades": 0,
    "low": 4.3131,
    "close": 4.3228,
    "high": 4.3326,
    "total_turnover": 2826041,
    "volume": 639124,
    "open": 4.3228
}

{
    "_id": "000016.XSHE_2020-01-02-09-33",
    "order_book_id": "000016.XSHE",
    "datetime": {
        "$date": "2020-01-02T09:33:00.000Z"
    },
    "num_trades": 0,
    "low": 4.3131,
    "close": 4.3228,
    "high": 4.3326,
    "total_turnover": 1087488,
    "volume": 246300,
    "open": 4.3131
}....

I am trying the following codes but none working:

tdcursor = MinutesDB.find({"order_book_id": "000016.XSHE", "datetime": {"$gte": recent11TradingDays[-1]},
                           "uplimit":{"$multiply":{"open": 1.1}}},
                          {"_id": 0, "order_book_id":1, "datetime": 1, "open": 1, "close": 1})
datadf = pd.DataFrame(tdcursor)

or

multiply = MinutesDB.aggregate([
    {"$group":
    {"order_book_id": "000016.XSHE",
    "uplimit": {"$multiply": ["$open", 1.1]}
    }
}
])
datadf = pd.DataFrame(multiply)

the code above gives error like below:

pymongo.errors.OperationFailure: The $in accumulator is a unary operator, full error: {'ok': 0.0, 'errmsg': 'The $in accumulator is a unary operator', 'code': 40237, 'codeName': 'Location40237'}

Thanks for your help!


Solution

  • here is a solution with an aggregate

    db.collection.aggregate([
      {
        "$match": {
          "order_book_id": "000016.XSHE",
          
        }
      },
      {
        "$project": {
          "uplimit": {
            "$multiply": [
              "$open",
              1.1
            ]
          }
        }
      }
    ])
    

    Since you wand to query only the documents that match : order_book_id": "000016.XSHE, you will need a $matchstage in the aggregate

    And then a $projectto use the $multiply

    Try it here