python - Mongo find 中 int 到 float 的转换

标签 python mongodb

如下面的代码所示,我在 mongo shell 中插入了 4 和 5。 PyMongo 将它们分为 4.0 和 5.0。我从 PyMongo 插入 7。 PyMongo 得到的结果是 7。

为什么会发生这种情况?我觉得这很阴暗,我不想在未来追寻无声的生产失败。 <在此插入咆哮>

剧透警告:我试图隐藏可能妨碍某些学生的信息。请小心。

来自蒙戈:

$:~/dev/mongouniv$ mongo
MongoDB shell version: 2.4.9
connecting to: test
> use m101
switched to db m101
> db.hw1.findOne()

> db.hw1.insert({"key":4})
> db.hw1.insert({"key":5})
> db.hw1.find()
{ "_id" : ObjectId("5566cccaa3e32a78d30eeddc"), "key" : 4 }
{ "_id" : ObjectId("5566ccdda3e32a78d30eeddd"), "key" : 5 }

来自Python:

import pymongo

from pymongo import MongoClient

# connect to database
connection = MongoClient('localhost', 27017)

In [20]: connection.m101.hw1.insert({"key":7})
Out[20]: ObjectId('5566ce513b8aa04a7585120f')

In [21]: list(connection.m101.hw1.find())
Out[21]: 
[
 {u'_id': ObjectId('5566cccaa3e32a78d30eeddc'), u'key': 4.0},
 {u'_id': ObjectId('5566ccdda3e32a78d30eeddd'), u'key': 5.0},
 {u'_id': ObjectId('5566ce513b8aa04a7585120f'), u'key': 7}]

我的 Ubuntu 软件包的版本:

dpkg -s python-pymongo
Version: 2.6.3-1build1
dpkg -s mongodb-server
Version: 1:2.4.9-1ubuntu2
dpkg -s mongodb-clients
Version: 1:2.4.9-1ubuntu2

最佳答案

http://docs.mongodb.org/manual/core/shell-types/ 可以看出:

By default, the mongo shell treats all numbers as floating-point values.

关于python - Mongo find 中 int 到 float 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30501202/

相关文章:

python - Django 干草堆/Solr : Show Solr field that is added in the prepare method of SearchIndex

python - 如何在其自己的文件夹中引用模块?

python - WTForms 小数点分隔符(适用于非英语应用程序)

mongodb - 完全外部加入 MongoDB

javascript - 为什么我的 NextJS 代码会运行多次,如何避免?

用于 Elasticsearch 的 mongodb river

python - 如何删除错误行错误的行并使用 pandas 或 numpy 读取剩余的 csv 文件?

python - 将字典写入文本文件?

mongodb.lock 权限被拒绝

node.js - 通过多种条件获取资源的最佳实践是什么?