nosql - 为什么mongodb的字段会有类型之分
阿神
阿神 2017-04-21 10:56:46
0
4
732

我想提出的问题是,mongodb既然是一个面向文档的数据库,而且它的存储格式又是与json如此类似的bson。如此宽松的使用环境,那么为什么不把步子放更大一点,而对每个字段都要有类型约束呢。比如下面的数据结构

{
    'field_1': 1,
    'field_2': '1',
    'field_3': true
}

在脚本语言里,这三个值是相等的,但mongodb可不这样认为,如果你用string类型去寻找field_1的值,那么永远找不到。

可是像在php这种弱类型语言里,本来就是string,int傻傻分不清楚,所以使用mongodb的php程序员经常会出现以下杯具场景,你存储的时候主键是一个用函数生成的int型数字,但取出记录的时候可能直接使用前端传递过来的GET参数,很不巧它只能是一个string类型,这样你就永远找不到这条记录了。

诸如此类的事情还很多,我在想既然mongodb不对数据结构做约束,为什么不干脆连数据类型也不做约束呢?因为既然不约束结构,那么程序中就无法对应出类型,这样也不好做强制类型转换之类的。你们有什么看法吗?

阿神
阿神

闭关修行中......

reply all(4)
刘奇

Retrieval efficiency and storage method can never be circumvented. The three types of storage byte lengths determine that the query and calculation efficiency cannot be consistent. No matter how nosql is optimized, it is only at the strategic level. After all, the storage layer is more What matters more is the performance of the medium itself.

迷茫

MongoDB is not only available for languages ​​like PHP.

刘奇

Perhaps it’s because of the C language. There is no data structure because the document is used for storage and bson is used. However, the specific data type is still strictly limited in the C language, unlike PHP’s data type language

大家讲道理

There are two sentences in

The Zen of Python (you can call import this in python cli to view):

Explicit is better than implicit.
Simple is better than complex.

The first sentence can explain the strong type of mongodb
The second sentence can explain mongodb’s schema-less

Python is like that. Dynamic language, but requires explicit type conversion. This is a more rigorous way of working.

Using PHP driver will indeed encounter this problem, and it can only be handled manually during query.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template