Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!