Python Mongo-find中int到float的转换
在下面的代码中,我在MongoShell中插入了4和5。PyMongo将其分为4.0和5.0。我从PyMongo插入7。PyMongo得到7分 为什么会这样?我觉得这很可疑,我不想在将来追查无声的生产失败在此处插入rant> 扰流板警告:我试图隐藏可能妨碍某些学生的信息。请小心 来自Mongo:Python Mongo-find中int到float的转换,python,mongodb,Python,Mongodb,在下面的代码中,我在MongoShell中插入了4和5。PyMongo将其分为4.0和5.0。我从PyMongo插入7。PyMongo得到7分 为什么会这样?我觉得这很可疑,我不想在将来追查无声的生产失败在此处插入rant> 扰流板警告:我试图隐藏可能妨碍某些学生的信息。请小心 来自Mongo: $:~/dev/mongouniv$ mongo MongoDB shell version: 2.4.9 connecting to: test > use m101 switched to d
$:~/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
如您所见:
默认情况下,mongo shell将所有数字视为浮点值
是的,你的4和5都是浮动的。尝试使用
numberprint
插入相同的数字-您应该会看到不同的结果。我用db.hw1.find({key:{$type:16}})
验证了其中key是int,用db.hw1.find({key:{$type:1})
验证了其中key是float。