Mongodb中一个有趣的数值查询案例

Mongodb集合中并没有强制所有文档具有统一的结构和字段类型。这样宽松的文档结构管理方法,给新数据的插入和以有数据的修改带来了极大的便利。但数据类型的频繁修改,可能参数查询和处理上的问题。

数值类型的变化, 是mongodb面临的一个问题之一。 项目设计之初,数值类型可能选择默认的整型。开发过程中数据的变化, 变成了浮点型。而浮点型不能够提供高精度计算,这字段类型又变成了小数类型decimal。针对不同数字类型的查询,mongodb有不同的结果。刚好在浏览mongodb文档时,发现了这个有趣的查询方法,整理并分享出来。

在types集合中,有5条数据,字段value值是不同的数据类型。包括整型,长整型,浮点型和小数类型decimal.

db.types.insertMany([
    {_id: 1, value: 1, expectedType: "Int32"},
    {_id: 2, value: Long("1"), expectedType: "Long"},
    {_id: 3, value: 1.01, expectedType: "Double"},
    {_id: 4, value: Decimal128("1.01"), expectedType: "Decimal128"},
    {_id: 5, value: 3200000001, expectedType: "Double"},
    ])
  • 查询value类型是整型的文档数据
db.types.find({value: {$type: "int"}})
{
	"_id" : 1,
	"value" : 1,
	"expectedType" : "Int32"
}
  • 查询value类型是长整型的文档数据
db.types.find({value: {$type: "long"}})
{
	"_id" : 2,
	"value" : Long("1"),
	"expectedType" : "Long"
}
  • 查询value类型是小数类型的文档数据
db.types.find({value: {$type: "decimal"}})
{
	"_id" : 4,
	"value" : Decimal128("1.01"),
	"expectedType" : "Decimal128"
}
  • 查询value类型是浮点数类型的文档数据
db.types.find({value: {$type: "double"}})
/* 1 */
{
	"_id" : 3,
	"value" : 1.01,
	"expectedType" : "Double"
},

/* 2 */
{
	"_id" : 5,
	"value" : Double("3200000001"),
	"expectedType" : "Double"
}
  • 查询value类型是数字类型的文档数据
db.types.find({value: {$type: "number"}})
/* 1 */
{
	"_id" : 1,
	"value" : 1,
	"expectedType" : "Int32"
},

/* 2 */
{
	"_id" : 2,
	"value" : Long("1"),
	"expectedType" : "Long"
},

/* 3 */
{
	"_id" : 3,
	"value" : 1.01,
	"expectedType" : "Double"
},

/* 4 */
{
	"_id" : 4,
	"value" : Decimal128("1.01"),
	"expectedType" : "Decimal128"
},

/* 5 */
{
	"_id" : 5,
	"value" : Double("3200000001"),
	"expectedType" : "Double"
}
  • 查询value值为1.01的文档数据
db.types.find({value: 1.01})
{
	"_id" : 3,
	"value" : 1.01,
	"expectedType" : "Double"
}
  • 查询value值为1的文档数据
db.types.find({value: 1})
/* 1 */
{
	"_id" : 1,
	"value" : 1,
	"expectedType" : "Int32"
},

/* 2 */
{
	"_id" : 2,
	"value" : Long("1"),
	"expectedType" : "Long"
}

几个查询语句中,最奇怪的当属查询值为1.01的查询语句了。 插入数据时, _id为3和_id为4的数据,数值上来看,都是1.01。但两个数值的类型不同。mongodb在使用{value: 1.01}查询时,隐式的将1.01转化为Double的类型。这样decimal128类型的数据就查不到了。

这里在程序开发过程中,是极易出错的点。在文档中看到了这个有趣的案例,就所幸记录下来。

相关推荐

  1. Mongodb一个有趣数值查询案例

    2023-12-30 10:08:03       51 阅读
  2. 一个有趣c++案例

    2023-12-30 10:08:03       22 阅读
  3. MongoDB 聚合查询数据统计应用

    2023-12-30 10:08:03       50 阅读
  4. Mongodb一个小巧数据更新命令$inc

    2023-12-30 10:08:03       30 阅读
  5. Mongodb查询投射$elemMatch

    2023-12-30 10:08:03       52 阅读
  6. 一个神奇SQL聚合查询案例

    2023-12-30 10:08:03       52 阅读
  7. OceanBase 一个关于 NOT IN 子查询 SQL 优化案例

    2023-12-30 10:08:03       27 阅读
  8. Mongodb老司机也会漏掉点——数组查询

    2023-12-30 10:08:03       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-30 10:08:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-30 10:08:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-30 10:08:03       82 阅读
  4. Python语言-面向对象

    2023-12-30 10:08:03       91 阅读

热门阅读

  1. TCP、IP、TCP/IP、HTTP和HTTPS协议简介

    2023-12-30 10:08:03       66 阅读
  2. h5 history模式是什么

    2023-12-30 10:08:03       49 阅读
  3. WPF 基础入门(XAML理解二)

    2023-12-30 10:08:03       57 阅读
  4. Linux 赛题FTP配置

    2023-12-30 10:08:03       52 阅读
  5. 第三篇 结构型设计模式 - 简化复杂系统的结构

    2023-12-30 10:08:03       53 阅读
  6. flutter实践:Isolate应用实例二

    2023-12-30 10:08:03       55 阅读
  7. 不同开源协议之间的差异分析

    2023-12-30 10:08:03       48 阅读
  8. go语言orm框架go-pg如何修改单列/多列、原地运算

    2023-12-30 10:08:03       51 阅读
  9. Spring Boot中关闭Job任务

    2023-12-30 10:08:03       56 阅读
  10. Spring + SpringMVC + SpringBoot

    2023-12-30 10:08:03       54 阅读
  11. TransRPPG

    TransRPPG

    2023-12-30 10:08:03      42 阅读
  12. 解决-bash: /usr/bin/mv: Argument list too long

    2023-12-30 10:08:03       56 阅读