如下面的代码所示,我在 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/