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

426
Visualizações
MongoDB + Python, unexpected numeric types

I have the following Python code:

from pymongo import MongoClient
db = MongoClient("mongodb://localhost/")["admin"]
collection = db["collection_test"]
collection.insert_one({"key1": 1000000000.1, "key2": 1.1})
doc = collection.find_one()
print(type(doc["key1"]))
print(type(doc["key2"]))

Which prints:

<class 'float'>
<class 'float'>

But based on the docs I expected the types to be

<class 'bson.decimal128.Decimal128'>

followed by something like 'bson.double'.

Why aren't I seeing a BSON Decimal128 and a BSON Double? Is type() not the correct way to get the types in this context?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Python floats are stored as BSON doubles. The API converts them on insertion and converts them back when querying.

If you want to store and retrieve BSON Decimal128 values, use bson.decimal128.Decimal128()

from bson.decimal128 import Decimal128

db = MongoClient()["mydatabase"]
collection = db["collection_test"]

collection.insert_one({"key1": Decimal128("1000000000.1"), "key2": Decimal128("1.1")})
doc = collection.find_one()
print(type(doc["key1"]))
print(type(doc["key2"]))

prints:

<class 'bson.decimal128.Decimal128'>
<class 'bson.decimal128.Decimal128'>

Documentation (albeit somewhat confusing): https://pymongo.readthedocs.io/en/stable/api/bson/index.html

Also: be careful of using the admin database; it's a special database in MongoDB.

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