在 Mongo find 中 int 到 float 转换 [英] int to float conversion in Mongo find

查看:25
本文介绍了在 Mongo find 中 int 到 float 转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我在 mongo shell 中插入了 4 和 5.PyMongo 将它们作为 4.0 和 5.0.我从 PyMongo 插入 7.PyMongo 得到它为 7.

As in the code below, I have inserted 4 and 5 in mongo shell. PyMongo gets them as 4.0 and 5.0. I insert 7 from PyMongo. PyMongo gets it as 7.

为什么会这样?我觉得这很阴暗,我不想在未来追逐无声的生产失败.<在这里插入咆哮>

Why is this happening? I find this shady and I dont want to chase silent production failures in the future. < insert rant here >

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

SPOILER WARNING: I have tried to hide information that may hamper certain students. Please be careful.

来自蒙戈:

$:~/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 软件包的版本:

Versions of my Ubuntu packages:

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/:

默认情况下,mongo shell 将所有数字视为浮点值.

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

这篇关于在 Mongo find 中 int 到 float 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆