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

425
Views
MongoDB + Python, tipos numéricos inesperados

Tengo el siguiente código de Python:

 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"]))

Que imprime:

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

Pero según los documentos , esperaba que los tipos fueran

 <class 'bson.decimal128.Decimal128'>

seguido de algo como 'bson.double'.

¿Por qué no veo un BSON Decimal128 y un BSON Double ? ¿ type() no es la forma correcta de obtener los tipos en este contexto?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Python float s se almacenan como BSON double s. La API los convierte al insertarlos y los vuelve a convertir al consultar.

Si desea almacenar y recuperar valores BSON Decimal128 , 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"]))

huellas dactilares:

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

Documentación (aunque algo confusa): https://pymongo.readthedocs.io/en/stable/api/bson/index.html

Además: tenga cuidado al usar la base de datos de admin ; es una base de datos especial en MongoDB.

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!