Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda